15

I am having a problem related to saving my Activity state. I searched and read about lots of questions here in SO but I couldn't get an answer for my question.

I have an Activity A with 2 Fragments. The Activity A holds the data which is showed by the Fragments. When I launch a new Intent for my settings Activity the Activity A is paused (not destroyed), onPause() and onSaveInstanceState() methods are called, so I save all my data in onSaveInstaceState().

When I return from my settings using back button Activity A is displayed again but onCreate() method is not being called because the Activity was not destroyed, instead onResume() method is called but I lost the state of my variables in Activity A and I can't access the Bundle I saved in onSaveInstanceState() because onCreate() is not called.

So onSaveInstanceState() is only useful when you rotate the screen? How can I access all the data I saved in onSaveInstanceState()? Or I should save them to a file or SharedPrefs to access them in onResume() later?

4

2 回答 2

29

这有帮助吗?
1. 使用getIntent().putExtras()inonStop()将您的数据保存到 Activity 的包中。
2.然后getIntent().getExtras()进去onResume()取回它。

你应该在访问包之前做一个空检查:)

于 2013-10-11T03:35:13.220 回答
-3

您可以将 SavedInstance 中的所有内容保存在包或变量中。

并在 onActivityCreated 方法上设置数据,如:

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        // Do your stuff here

}
于 2013-10-11T04:10:44.363 回答