Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lifecycle when calling the fragment replace or open new activity?

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);
            }
        }
    }
like image 843
user3538235 Avatar asked Dec 14 '22 09:12

user3538235


1 Answers

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.

like image 176
Ludwig S Avatar answered Jan 31 '23 07:01

Ludwig S