[英]Maintain Activity state when changing to a different Activity
I have the following scenario: 我有以下情况:
What i would like to do is keep the state of Activity A when clicking Activity B's back button. 我想做的是单击“活动B”的“后退”按钮时保持活动A的状态。
Activity A code: 活动A代码:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null)
{
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
// check for saved instance
if (savedInstanceState != null)
{
//restore saved values
}
else
{
//initialize members with default values
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
savedInstanceState.putString("typeID", typeID);
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
typeID = savedInstanceState.getString("typeID");
}
public boolean gotoActivityB(View view)
{
Intent intent = new Intent(getApplicationContext(), ActivityB.class);
startActivity(intent);
return false;
}
Activity B code: 活动B代码:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buy_item);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null)
{
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
//do some magic...
}
public boolean onOptionsItemSelected(MenuItem item)
{
Intent intent = new Intent(getApplicationContext(), ActivityA.class);
startActivity(intent);
return false;
}
Every time i go from Activity B to Activity A, savedInstanceState is equal to null, in other words, Activity A state isn't saved or restored. 每当我从活动B转到活动A时,savedInstanceState等于null,换句话说,活动A状态不会保存或恢复。
What am i missing here? 我在这里想念什么?
!!SOLUTION!! !!解!!
Based on @cybersam 's answer, Activities maintain their state by default. 根据@cybersam的答案,活动默认情况下保持其状态。 So there is no need for the savedInstanceState. 因此,无需使用savedInstanceState。 To solve my problem i only had to update my back button events to: 为了解决我的问题,我只需要将后退按钮事件更新为:
public boolean onOptionsItemSelected(MenuItem item)
{
finish();
return true;
}
As the documentation for onSaveInstanceState() states: 正如onSaveInstanceState()的文档所述:
An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact. 在调用onPause()而不是onSaveInstanceState(Bundle)时的一个示例是在活动A的前面启动活动B时:如果在活动B的生命周期内未将其杀死,则系统可以避免在活动A上调用onSaveInstanceState(Bundle) A的用户界面状态将保持不变。
So you cannot assume that onSaveInstanceState()
would be called on A
just because B
is launched in front of it. 因此,您不能仅仅因为B
在其前面启动而就假定onSaveInstanceState()
将在A
上被调用。 In fact, most of the time, it will not be. 实际上,大多数情况下不会。
[EDITED] [EDITED]
Your code for B seems to be calling startActivity() to "go back" to the prior activity. 您的B代码似乎正在调用startActivity()以“返回”先前的活动。 If you just want B to go back to the prior activity, you can (usually) just call finish() to exit B, which should allow A to reappear (with its state intact), since it will become the top Activity in the stack . 如果您只想让B返回到先前的活动,则可以(通常)仅调用finish()退出B,这将允许A重新出现(状态不变),因为它将成为堆栈中的顶级Activity 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.