4

The is an fragment that display the video.

This fragment can either

1) open a new activity on click button

2) replace with another fragment by calling

fragmentManager.beginTransaction().replace(R.id.container, f).addToBackStack(tag).commit();

for the 1) case , I would like to call player.stopPlayBack() to stop the video playing at backing

And for the 2) case , I would like to call player.stopPlayBack() and player.release() to terminate the player

The problem is , what event I should call for the case 1) and 2)? I try using onPause or onStop but both of them seems has not fired.

How to fix it?

Thanks a lot for helping.

Updated:

Video fragment code

public class Video extends Fragment implements MediaPlayer.OnPreparedListener {
    @Bind(R.id.player) EMVideoView player;
    @Bind(R.id.full_screen) ImageView full_screen;
    Context ctx;
    MyApp app;
    String video_url;
    int intent_code = 5545;
    int pos;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.video, container, false);
        ButterKnife.bind(this, view);

        Bundle bundle = this.getArguments();
        video_url = bundle.getString("video_url");
        String id = bundle.getString("id");

        app = (MyApp) getActivity().getApplicationContext();
        app.record_view(id);

        Main m = (Main)getActivity();
        m.toggle_upload_btn(false);

        pos = 0;

        full_screen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getActivity(), VideoFullScreen.class);
                i.putExtra("video_url", video_url);
                i.putExtra("time", (int) player.getCurrentPosition());
                startActivityForResult(i, intent_code); //random intent number
            }
        });

        return view;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        player.seekTo(pos);
        player.start();
    }

    @Override
    public void onResume() {
        super.onResume();
        player.setOnPreparedListener(this);
        player.setVideoURI(Uri.parse(video_url));
    }

    @Override
    public void onStop() {
        super.onStop();
        player.stopPlayback();
        //player.release();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == intent_code) {
            if(resultCode == Activity.RESULT_OK){
                pos = data.getIntExtra("time", 0);
            }
        }
    }
user3538235
  • 1,991
  • 6
  • 27
  • 55
  • Are there two buttons, one for case 1, and one for case 2? Why not handle your different cases in their respective onClickListeners if so? – Ludwig S Jan 14 '16 at 07:42
  • just reading the doc and have some idea, is onpause for the 1) case and onDetach for 2) case? – user3538235 Jan 14 '16 at 07:46

1 Answers1

12

When the fragment is added to the backstack, and then getting replaced or removed - it will go like this:

onPause() -> onSaveInstanceState() -> onStop() -> onDestroyView() 

If the fragment is removed, or replaced without getting added to the back stack, then following happens:

onPause() -> onSaveInstanceState() -> onStop() -> onDestroyView()  -> onDestroy() -> onDetach() -> Fragment is destroyed.

And when a activity starts another activity (source):

The order of lifecycle callbacks is well defined, particularly when the two activities are in the same process and one is starting the other. Here's the order of operations that occur when Activity A starts Acivity B:

Activity A's onPause() method executes. Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.) Then, if Activity A is no longer visible on screen, its onStop() method executes.

Because you need to call on your activity where your fragment is existing, to start a new activity.

Ludwig S
  • 551
  • 3
  • 8
  • that means either a fragment create new activity or replace fragment will go over the same event? – user3538235 Jan 14 '16 at 09:14
  • 1
    Yes, so you shouldn't control the playback of that particular case in your lifecycle methods. – Ludwig S Jan 14 '16 at 09:18
  • Thanks for reply. Also, I stated the video.stopplayback() at onStop, and when I use fragment replace , the video does not stop , and continue playing, so if I replace more fragment then more video play at same time, any idea to fix it? thanks – user3538235 Jan 14 '16 at 09:33
  • What is the object player? What is it that you stopPlayback() on? – Ludwig S Jan 14 '16 at 09:46
  • From what I can tell, it should work - and after reading on EmVideoView's documentation that was very shallow, it looks like it should be working. Could you try to use that stopPlayBack() inside your imageviewClickListener? And just comment out the intent, to see if that works. – Ludwig S Jan 14 '16 at 10:05