调用片段替换或打开新活动时的生命周期? [英] Lifecycle when calling the fragment replace or open new activity?

查看:73
本文介绍了调用片段替换或打开新活动时的生命周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是显示视频的片段.

此片段可以

1)点击按钮打开一个新活动

1) open a new activity on click button

2)通过调用

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

对于1)情况,我想打player.stopPlayBack()来停止支持播放视频

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

对于2)情况,我想调用player.stopPlayBack()player.release()终止播放器

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

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

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.

如何解决?

非常感谢您的帮助.

已更新:

视频片段代码

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

推荐答案

将片段添加到后堆栈中,然后被替换或移除-它将像这样:

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):

生命周期回调的顺序已明确定义,尤其是在以下情况下 两项活动都在同一过程中,一项正在启动 其他.这是活动A时发生的操作顺序 启动Acivity B:

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:

执行活动A的onPause()方法.活动B的onCreate(), onStart()和onResume()方法按顺序执行. (现在进行活动B 以用户为中心.)然后,如果活动A"在屏幕上不再可见, 它的onStop()方法执行.

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.

这篇关于调用片段替换或打开新活动时的生命周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆