简体   繁体   中英

How to properly save and restore Activity and Fragment status in Android?

In Android, Activity and Fragment can be destroyed at any time (if, for example, user has navigated away from the Activity or another Activity has been called). I have some data in the Fragment and I want to save it, but I can not find a proper way to do it.

For example, in the Fragment, I have this code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (null != savedInstanceState) 
        initState(savedInstanceState);
    else 
        initStage(getArguments());     
}

private void initState(Bundle bundle) {
    mData = bundle.getParcelable(ARG_DATA);
} 

public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putParcelable(ARG_DATA, mData);
}

And in Activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_container);

    if (findViewById(R.id.fragment_container) != null) {
        Fragment fragment = getSupportFragmentManager()
            .findFragmentById(R.id.fragment_container);
        if (null == fragment) {
            MyData data = getIntent().getParcelableExtra(ARG_DATA);

            fragment = MyFragment.newInstance(data);
            fragment.setRetainInstance(true);

            getSupportFragmentManager()
                 .beginTransaction()
                 .replace(R.id.fragment_container, fragment)
                 .commit();
        }
    }
}

But what will happen if hosting Activity and it's Fragment will be destroyed and created again? Will it recreate the Fragment and try to initialize it with old arguments? How to avoid that, do I have to transfer the data back to the Activity every time, then it will be destroyed? What is the point of having the Fragment, then?

Thanks

Update.

To explain the problem, I have a Service working to keep core data in it. I only need Activity to present the data, for example to create a new record in db. But what I have found out initially, is what if Activity gets destroyed and recreated, it will also recreate the underlying Fragment and will initialize it with data, sitting in the Activity arguments bundle. And all user changes will be lost. What I want is that after Fragment gets created, it will store the data, submitted by user until user will hit "submit" button and exit from the Activity. So, even if user navigates away from the activity and then come back, he would not lost the data he already entered. The problem is, I can not update the record straight after user entered something. It is complicated system, the record not only go to the local db, the service will also send it to the server and I want to minimise traffic going between phone and the server.

I answered a similar question on SO @ Fragment calling fragments loosing state on screen rotation . Search for my user name "The Original Android".

There is a difference in the sample code, compared to your code, that is the main Activity has the saved data or states . The reason is the Fragment data may be destroyed, gone from memory. But main activity gets destroyed when user exits the app, and the lifecycle ends for the app in general. So it is safer to store data in the Main Activity.

EDIT : If you want to read up on Fragment lifecycle, it is @ Fragments .

Good luck, keep us posted...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM