java 调用 replace() 时片段的生命周期是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25203360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:34:14  来源:igfitidea点击:

What is the lifecycle of a fragment when replace() is called?

javaandroidandroid-fragmentsfragment

提问by Leon

I have a number of fragments which are dynamically added using the following code:

我有许多使用以下代码动态添加的片段:

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    if(position == 0){
        fragment = new FirstFragment();
    }
    else if(position == 1){
        fragment = new SecondFragment();
    }
    else if(position == 2){
        fragment = new ThirdFragment();
    }

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mCalculatorTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

In one of my fragments I have a timer and I've just discovered that when the fragment is replaced the timer in the old fragment is still running. Where in my fragment's lifecycle should I kill the timer?

在我的一个片段中,我有一个计时器,我刚刚发现当片段被替换时,旧片段中的计时器仍在运行。我应该在片段生命周期的哪个位置终止计时器?

EDIT:

编辑:

Okay so I added a timer.cancel() in the fragment's onStop() method but onStop() also gets called when I load the preferences from a button on the action bar. This is not the desired effect. Any other ideas?

好的,所以我在片段的 onStop() 方法中添加了一个 timer.cancel() 但是当我从操作栏上的按钮加载首选项时也会调用 onStop() 。这不是想要的效果。还有其他想法吗?

采纳答案by pjco

I would kill any timers or Async work in onStop()

我会杀死任何计时器或异步工作 onStop()

http://developer.android.com/guide/components/fragments.html#Lifecycle

http://developer.android.com/guide/components/fragments.html#Lifecycle

Quoting the API doc:

引用 API 文档:

Stopped
The fragment is not visible. Either the host activity has been stopped or the fragment has been removed from the activity but added to the back stack. A stopped fragment is still alive (all state and member information is retained by the system). However, it is no longer visible to the user and will be killed if the activity is killed.

已停止
片段不可见。主机活动已停止或片段已从活动中删除但已添加到返回堆栈中。停止的片段仍然存在(系统保留所有状态和成员信息)。但是,它不再对用户可见,如果活动被终止,它将被终止。

Fragment lifecycle:

片段生命周期:

Fragment lifecycle

片段生命周期

回答by Fatih S.

Timer task is not related with fragment life cycle. You can cancel timer when you finished your job with that fragment. Fragment's onStop method is good place to do that.

定时器任务与片段生命周期无关。完成该片段的工作后,您可以取消计时器。Fragment 的 onStop 方法是这样做的好地方。

public void onStop() {
    super.onStop();
    timer.cancel();
}