调用片段替换或打开新时的生命周期activity?

Lifecycle when calling the fragment replace or open new activity?

这是一个显示视频的片段。

这个片段可以

1) 打开一个新的 activity 点击按钮

2) 通过调用

替换为另一个片段
fragmentManager.beginTransaction().replace(R.id.container, f).addToBackStack(tag).commit();

对于 1) 的情况,我想调用 player.stopPlayBack() 停止播放视频

对于第 2) 种情况,我想调用 player.stopPlayBack()player.release() 来终止玩家

问题是,对于情况 1) 和 2) 我应该调用什么事件?我尝试使用 onPause 或 onStop 但它们似乎都没有被触发。

如何解决?

非常感谢您的帮助。

Updated:

视频片段代码

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);
            }
        }
    }

当片段被添加到后台堆栈,然后被替换或删除时 - 它会像这样:

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

如果片段被删除或替换而没有添加到返回堆栈,则会发生以下情况:

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

并且当 activity 启动另一个 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.

因为您需要调用片段所在的 activity 来开始新的 activity。