的onCreate被要求活动A有向上导航 [英] onCreate being called on Activity A in up navigation
问题描述
所以,我有一个活动A和活动B.我想一个活动要能够导航到b活动与按钮的preSS。这样的作品,但是当我使用了导航(操作栏中的home键)来导航回到活动A,的onCreate()再次打来电话,旧的信息,用户键入的丢失。
So I have an Activity A and an Activity B. I want Activity A to be able to navigate to Activity B with the press of a button. That works, but when I use the up navigation(the home button in the action bar) to navigate back to Activity A, onCreate() is called again and the old information that the user typed in is lost.
我见过的:<一href="http://stackoverflow.com/questions/11347161/oncreate-always-called-if-navigating-back-with-intent">onCreate总是叫如果导航回意图,但他们使用的碎片,我希望不要有重新设计整个应用程序使用的片段。有没有什么办法可以被称为每次活动一再次激活阻止的onCreate()?
I've seen: onCreate always called if navigating back with intent, but they used Fragments, and I'm hoping not to have to redesign the entire app to use fragments. Is there any way I can stop onCreate() from being called every time Activity A becomes active again?
推荐答案
此行为是完全正常和希望。
该系统可能会停止对活动
这是在后台释放一些内存。
同样的事情发生,例如当旋转设备
This behavior is totally fine and wanted.
The system might decide to stop Activities
which are in background to free some memory.
The same thing happens, when e.g. rotating the device.
通常你保存你的实例状态(如输入的文本和材料),以一个包,并从当活动
被重新捆绑获取这些值。
Normally you save your instance state (like entered text and stuff) to a bundle and fetch these values from the bundle when the Activity
is recreated.
下面是一些标准的code我用:
Here is some standard code I use:
private EditText mSomeUserInput;
private int mSomeExampleField;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO inflate layout and stuff
mSomeUserInput = (EditText) findViewById(R.id.some_view_id);
if (savedInstanceState == null) {
// TODO instanciate default values
mSomeExampleField = 42;
} else {
// TODO read instance state from savedInstanceState
// and set values to views and private fields
mSomeUserInput.setText(savedInstanceState.getString("mSomeUserInput"));
mSomeExampleField = savedInstanceState.getInt("mSomeExampleField");
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// TODO save your instance to outState
outState.putString("mSomeUserInput", mSomeUserInput.getText().toString());
outState.putInt("mSomeExampleField", mSomeExampleField);
}
这篇关于的onCreate被要求活动A有向上导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!