18

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?

Leon
  • 3,614
  • 1
  • 33
  • 46
  • Check out - https://androidlearnersite.wordpress.com/2017/02/27/fragment-lifecycle-during-fragment-transaction/ ..It explains fragment lifecycle during fragment transaction with latest appcompat version – Android Developer Feb 27 '17 at 08:56

2 Answers2

12

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

pjco
  • 3,826
  • 25
  • 25
  • 2
    Okay this seems to correlate with what's happening but is there anything that gets called when the fragment is replaced within the same activity? – Leon Aug 08 '14 at 16:41
  • All of those should be called when a fragment is replaced. One way to demonstrate this is to override each life-cycle method and `Log` them. Then you can watch the various callbacks. Hope that helps. – pjco Aug 08 '14 at 16:55
  • 1
    Sorry I didn't ask the right question. Is there anything that gets called ONLY when the fragment is replaced? I am also loading the preferences activity on top and I don't really want the timer to stop during this time. onStop() gets called however. – Leon Aug 08 '14 at 17:00
  • Nothing specific to `replace()`, no. But you can create your own interface called `Replaceable` with a method `onReplace()` that you have your fragment implement, and call it directly when you call `FragmentTransaction.replace()`. – pjco Aug 21 '14 at 01:43
2

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();
}
Fatih S.
  • 341
  • 2
  • 8
  • This was my first thought but it's not completely right, onStop() also gets called when I add a new activity on top, see my edit. – Leon Aug 08 '14 at 16:40