Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Duplicating of fragments when using Support Fragment Manager

I have a very weird issue with fragments. I use the newest support library. I also use similar code as is used by Google in their IOSCHED project

But I have problem with recreating of activity after rotation. After an activity is destroyed and created again, I call methods for managing the fragment transaction (in onStart method). This line is called only once, but it creates the fragment TWICE !

This is my activity method:

public abstract class SinglePaneActivity extends FragmentActivity
{
    @Override
    protected void onStart()
    {
        super.onStart();

        if(mFragment == null)
        {
            mFragment = onCreatePane();     
            mFragment.setArguments(Utils.intentToFragmentArguments(getIntent()));

            Log.w(TAG, "Fragment creation counter = " + createCounter);
            createCounter++;

            getSupportFragmentManager() 
            .beginTransaction()
            .add(R.id.root_container,mFragment)
            .commit();
        }
    }

    @Override
    protected void onStop()
    {
        Log.i(TAG, "onStop");
        if(mFragment != null)
        {
            getSupportFragmentManager()
            .beginTransaction()
            .remove(mFragment)
            .commit();

            mFragment = null;
        }
        super.onStop();
    }
}

and my logs:

--Start of application--
11-18 13:26:37.050: I/SinglePaneActivity(19040): onCreate
11-18 13:26:37.050: I/SinglePaneActivity(19040): onStart
11-18 13:26:37.055: W/SinglePaneActivity(19040): replacing fragment, counter = 1
11-18 13:26:37.075: I/MyFragment(19040): onCreate
11-18 13:26:37.110: I/MyFragment(19040): onActivityCreated
--Rotating the device--
11-18 13:26:39.600: I/SinglePaneActivity(19040): onStop
11-18 13:26:39.600: I/SinglePaneActivity(19040): onDestroy
11-18 13:26:39.605: I/MyFragment(19040): onDestroy
11-18 13:26:39.755: I/MyFragment(19040): onCreate
11-18 13:26:39.755: I/SinglePaneActivity(19040): onCreate
11-18 13:26:39.790: I/MyFragment(19040): onActivityCreated
11-18 13:26:39.800: I/SinglePaneActivity(19040): onStart
11-18 13:26:39.800: W/SinglePaneActivity(19040): replacing fragment, counter = 2
11-18 13:26:39.810: I/MyFragment(19040): onCreate
11-18 13:26:39.815: I/MyFragment(19040): onActivityCreated
--Rotating the device back--
11-18 13:36:47.060: I/SinglePaneActivity(19040): onStop
11-18 13:36:47.060: I/SinglePaneActivity(19040): onDestroy
11-18 13:36:47.060: I/MyFragment(19040): onDestroy
11-18 13:36:47.065: I/MyFragment(19040): onDestroy
11-18 13:36:47.130: I/MyFragment(19040): onCreate
11-18 13:36:47.130: I/MyFragment(19040): onCreate
11-18 13:36:47.130: I/SinglePaneActivity(19040): onCreate
11-18 13:36:47.140: I/MyFragment(19040): onActivityCreated
11-18 13:36:47.150: I/MyFragment(19040): onActivityCreated
11-18 13:36:47.150: I/SinglePaneActivity(19040): onStart
11-18 13:36:47.150: W/SinglePaneActivity(19040): replacing fragment, counter = 3
11-18 13:36:47.160: I/MyFragment(19040): onCreate
11-18 13:36:47.160: I/MyFragment(19040): onActivityCreated
--Exiting the app--
11-18 13:36:48.880: I/SinglePaneActivity(19040): onStop
11-18 13:36:48.885: I/SinglePaneActivity(19040): onDestroy
11-18 13:36:48.885: I/MyFragment(19040): onDestroy
11-18 13:36:48.890: I/MyFragment(19040): onDestroy
11-18 13:36:48.890: I/MyFragment(19040): onDestroy

So the number of fragments is growing after each rotation.

After rotation it restores the fragment before it goes to my onStart method and my onStart method creates second same fragment in same Frame layout container. But WHERE it restores the first fragment ? I want to forbit it. Or should I change my "if" to test if it is already created? But I dont know how to determine it. It looks that null test is useless.

I also made small workaround by replacing add() method by replace(). After that, the number of fragments isn't growing and each fragment is destroyed before new one is in onStart method created. But this makes serious problem in some fragments where I start some background processes in theirs onCreate method...

Please help me...I really have no idea what to do with it. Thank you very much for any advice or idea !

like image 378
Bhiefer Avatar asked Nov 18 '11 13:11

Bhiefer


2 Answers

Well I found a solution. I had to move the code with fragment manager from onStart to onCreate and check null value of savedInstanceState. Well I didn't know that it is saved and restored from savedInstanceState automagically. Good to know!

EDIT: And to do it completely correct, i.e. restore fragment when savedInstanceState is not null, there should be:

    if(savedInstanceState == null)
    {
        mFragment = onCreatePane();     
        mFragment.setArguments(Utils.intentToFragmentArguments(getIntent()));

        getSupportFragmentManager() 
        .beginTransaction()
        .add(R.id.root_container,mFragment)
        .commit();
    } 
    else
    {
        mFragment = getSupportFragmentManager().findFragmentById(R.id.root_container);
    }
like image 76
Bhiefer Avatar answered Oct 18 '22 20:10

Bhiefer


I had a similar problem, but I was adding a child fragment from a parent fragment and seeing a duplicate child fragment. I was trying to add it in onStart of the parent fragment so I could access the parent view, which isn't available in onCreate. So instead of moving the code to onCreate, I moved it to onActivityCreated; that method has access to the parent view and also has access to the savedInstanceState variable to check if the fragment is already being added automatically. This isn't a problem when adding a fragment from an activity because the activity's view is available in onCreate.


If for some reason you need to add a fragment in onStart or another place where savedInstanceState is not available, here's an alternative. Using the original onStart code from the question...

   if(mFragment == null)
    {
        mFragment = onCreatePane();     
        mFragment.setArguments(Utils.intentToFragmentArguments(getIntent()));

        Log.w(TAG, "Fragment creation counter = " + createCounter);
        createCounter++;

        getSupportFragmentManager() 
        .beginTransaction()
        .add(R.id.root_container,mFragment)
        .commit();
    }

...you could replace rather than add the fragment:

   if(mFragment == null)
    {
        mFragment = onCreatePane();     
        mFragment.setArguments(Utils.intentToFragmentArguments(getIntent()));

        Log.w(TAG, "Fragment creation counter = " + createCounter);
        createCounter++;
    }

    getSupportFragmentManager() 
    .beginTransaction()
    .replace(R.id.root_container,mFragment)
    .commit();

Then if the fragment was already added automatically, your new fragment will just replace it in the view.

like image 40
arlomedia Avatar answered Oct 18 '22 21:10

arlomedia