L
L
Lord Drous2016-11-09 19:26:08
Java
Lord Drous, 2016-11-09 19:26:08

How, when switching to another fragment, stop the actions in the previous one?

Hello, I have 2 fragments MainFragment(1) and GalleryFragment(2), in fragment 2 I have a media player, the play button starts broadcasting, after which I press home, but the broadcast does not stop. How to make it stop when switching to another fragment? 0ba07c721b3946e3a73423cc4678613a.jpg6ab9a9e7d6c64f79be32ec228f8afc4d.jpg
GalleryFragment code. not to 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.LayoutManager mLayoutManager = 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.play1);
        playPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer == null){
                    mediaPlayer = new MediaPlayer();
                    Toast.makeText(getActivity(), "Идет подключение к радиостанции", 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(), "Идет подключение к радиостанции..", 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,"Радио Асса", "104,8 FM");
        itemList.add(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() {
      
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rou1997, 2016-11-09
@Rou1997

then I press home, but the broadcast does not stop
Home or back?
If you are home, then you can do something, but it is illogical to stop it at the same time, this is minimizing the application, not closing it! I once fought in the Windows Store on the contrary so that the minimized application continued to play.
If back, then "how" - "hands", that is, in onDestroyView()the Fragment from which you leave, or in another event, see the Fragment life cycle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question