Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 694
Leon Avatar asked Aug 08 '14 12:08

Leon


People also ask

Which method is called when fragment is replaced?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();

What is the life cycle of fragment?

Android fragment lifecycle is affected by activity lifecycle because fragments are included in activity. Each fragment has its own life cycle methods that is affected by activity life cycle because fragments are embedded in activity. The FragmentManager class is responsible to make interaction between fragment objects.

What lifecycle methods will trigger when Fragmentb replaces fragmenta?

When Fragment B replaces Fragment A,Fragment A is destroyed and Fragment B is created. However in case the transaction that had added Fragment A was saved using addToBackStack method,then backstack is holding reference to that fragment from previous transaction and hence only its view is destroyed.


1 Answers

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

like image 142
pjco Avatar answered Oct 11 '22 17:10

pjco