当replace()被调用时,一个片段的生命周期是什么?[英] What is the lifecycle of a fragment when replace() is called?

本文是小编为大家收集整理的关于当replace()被调用时,一个片段的生命周期是什么?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

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

在我的一个片段中,我有一个计时器,我刚刚发现,当片段被替换时,旧片段中的计时器仍在运行.我在片段的生命周期中应该在哪里杀死计时器?

编辑:

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

推荐答案

我会杀死onStop()

中的任何计时器或异步工作

http://developer.android.com/guide.com/guide.com/guide/components/fragments/fragments. html#生命周期

引用API文档:

停止
片段不可见.主机活动已停止,或者片段已从活动中删除,但添加了 到后堆.停止的碎片仍然活着(所有状态和 会员信息由系统保留).但是,这不是 用户可见的时间更长,如果活动是 被杀.

片段生命周期:

 fragment lifecycle

其他推荐答案

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

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

本文地址:https://www.itbaoku.cn/post/2091034.html

问题描述

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?

推荐答案

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

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

Quoting the API doc:

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

其他推荐答案

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.

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