How do fragments of an activity get restored when the activity restarts?

Chase Farmer :

I am testing situation where a user goes into my app after the system has terminated the app process due to low RAM. I am seeing unexpected behavior and hoping to get some help.

In my app, I have an activity, lets call it ActivityA, that immediately creates a fragment, Fragment A, and does a fragment replacement. FragmentA displays a ListView with two items in it. If the user clicks the first item, a second fragment, Fragment B is created and replaces FragmentA. Otherwise, another FragmentA is created and replaces the original FragmentA. I'm trying to create a file directory tree. FragmentA is for directories, and FragmentB is for files.

Lets say the user clicks on a file. This is the stage in the test where the user switches to another app, and the system terminates my app's process due to low memory. Then, the user goes back into my app expecting everything to be left the way it was. However, what actually happens is Fragment A (The parent directory) is being displayed instead of Fragment B (the file). When the user clicks the back button, Fragment B (the file) is then displayed. What am I doing wrong that is causing the system to restore the backstack in this way?

Here is an example program to further show what my app is doing:

// ActivityA.java
public class ActivityA extends AppCompatActivity implements onItemClickListener
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        FragmentA fragA = new FragmentA();
        FragmentTransaction fragmentTransaction = 
            getSupportFragmentManager().beginTransaction();
        fragmentTransation.replace(R.id.basic_frame, fragA);
        fragmentTransaction.commit();
    }

    @Override
    public void onItemClick(AdapterView<?> aView, View v, int position, long id)
    {
        if (position == 0)
        {
            FragmentB fragB = new FragmentB();
            FragmentTransaction fragmentTransaction = 
                getSupportFragmentManager().beginTransaction();
            fragmentTransation.replace(R.id.basic_frame, fragB);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
        else
        {
            FragmentB fragA = new FragmentA();
            FragmentTransaction fragmentTransaction = 
                getSupportFragmentManager().beginTransaction();
            fragmentTransation.replace(R.id.basic_frame, fragA);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    }
}
ianhanniballake :

When you call super.onCreate(), Fragments automatically restore their current state when savedInstanceState is not null.

Therefore if you're expecting to do one time set up by adding your intial Fragment, you should always surround it with an if (savedInstanceState == null) check:

@Override
public void onCreate(Bundle savedInstanceState)
{
    // I assume you accidentally left out these lines
    super.onCreate(savedInstanceState);
    setContentView(R.id.your_content_view);

    if (savedInstanceState == null) {
        FragmentA fragA = new FragmentA();
        FragmentTransaction fragmentTransaction = 
        getSupportFragmentManager().beginTransaction();
        fragmentTransation.replace(R.id.basic_frame, fragA);
        fragmentTransaction.commit();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=148883&siteId=1