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

后端 未结 2 921
夕颜
夕颜 2021-02-12 23:34

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

private class DrawerItemClickListener implements ListView.OnItemClickListener          


        
相关标签:
2条回答
  • 2021-02-12 23:59

    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

    0 讨论(0)
  • 2021-02-13 00:16

    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();
    }
    
    0 讨论(0)
提交回复
热议问题