As when switching to another fragment, to stop the actions in predyduschem?

Hello, I have 2 fragments MainFragment(1) and GalleryFragment(2), in the 2nd snippet, I have a media player, the play button starts the broadcast, and then press home, but the broadcast is not stopped. How to make to stop when switching to another fragment?0ba07c721b3946e3a73423cc4678613a.jpg6ab9a9e7d6c64f79be32ec228f8afc4d.jpg
code GalleryFragment. not until the end:
public class AsaFragment extends Fragment {
 private static final String LOG_TAG = "myLog";
 private List<item> itemList = new ArrayList<>();
 private RecyclerView recyclerView;
 private ToggleButton playPause;
 private ToggleButton stopButton;
 //private ToggleButton startButton;
 private MyAdapter myAdapter;
 private MediaPlayer mediaPlayer;
 private String STREAM_URL = "http://stream3.radiostyle.ru:8003/radioacca";

 public AsaFragment() {
 // Required empty public constructor
}

@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) {
 // Inflate the layout for this fragment

 View View = inflater.inflate(R. layout.fragment_asa, container, false);
 recyclerView = (RecyclerView)view.findViewById(R. id.recycler_view1);
 myAdapter = new MyAdapter(itemList);
//myAdapter.setClickListener(this);
 RecyclerView.MLayoutManager LayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
 recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(myAdapter);
prepareItem();
 playPause = (ToggleButton)view.findViewById(R. id.playpause1);
 stopButton = (ToggleButton)view.findViewById(R. id.stopButton1);
 //startButton = (ToggleButton)view.findViewById(R. id.1);
 playPause.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View View) {
 if (mediaPlayer == null){
 mediaPlayer = new MediaPlayer();
 Toast.makeText(getActivity(), "connecting to radio", Toast.LENGTH_LONG).show();
 try {
mediaPlayer.reset();
mediaPlayer.setDataSource(STREAM_URL);
mediaPlayer.prepareAsync();
 mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
 public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
 } catch (IOException e) {
e.printStackTrace();
 Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
}
}
 else if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
 } else {
mediaPlayer.start();
}
 //Toast.makeText(getActivity(), "connecting to radio station..", Toast.LENGTH_LONG).show();
}
});
registerForContextMenu(playPause);
 stopButton.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View View) {
 if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
 mediaPlayer = null;
playPause.setChecked(false);
}
 else {
playPause.setChecked(true);
}
}

});
 playPause.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
 public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
 if (compoundButton.getId() == playPause.getId() && b){
playPause.setBackgroundResource(R. drawable.toggle_playback);
}
}
});
registerForContextMenu(stopButton);
 return view;
}
 /*public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
 playPause.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
 public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
 if (compoundButton.getId() == playPause.getId() && b){
playPause.setChecked(true);
}
}
});
 } */
 private void prepareItem() {
 Item item = new Item(R. drawable.assa,"Radio AssA", "104,8 FM");
itemList.add(item);
 }</item>

MainFragment:
public class MainFragment extends AsaFragment {

 public MainFragment() {
 // Required empty public constructor
}
@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) {
 // Inflate the layout for this fragment
 return inflater.inflate(R. layout.fragment_main, container, false);
}
@Override
 public void onDestroyView(){
super.onDestroyView();
stopRadio();
}

 private void stopRadio() {

}

}
July 2nd 19 at 18:10
1 answer
July 2nd 19 at 18:12
then hit home, but the broadcasting does not stop
Home or back?
If home, then it is possible to do, but it is illogical to stop this hiding, and not closing! I once in the Windows Store fought contrary to a minimized application continued to play.
If ago, the "how", "hands", that is, in onDestroyView() of that Fragment and from which you leave, or in another event, see life cycle of Fragment.
sorry, I'm very sensitive you don't properly explained, on the screen there is a navigation drawer, where the home button had a view as when you click on it, pause, which is in another fragment - nicolette commented on July 2nd 19 at 18:15
: And what's the problem? You can't handle this menu?
It is in the NavigationView, so the treatment is also realized through it, like this:
navigationView1.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
 public boolean onNavigationItemSelected(MenuItem menuItem) {
 //itemId contains the id of the selected item, you need to verify with navigationView1.getMenu()
 return true;
}
});

Of course, navigationView1 first need to be initialized via findViewById() , etc. - mazie.Towne commented on July 2nd 19 at 18:18

Find more questions by tags JavaAndroid