我对活动A、B、C有以下情况: A->B->C->A
在最后一步( C ->A)中,我想覆盖C的onBackPressed,以便它重新启动activity A(不重新创建它)。我应该添加哪个标志?
public void onBackPressed() {
Intent intent=new Intent(C.this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}发布于 2016-07-15 20:38:56
这是刷新活动的最佳方式:
public void refresh() {
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}发布于 2016-07-15 20:05:08
OnCreate将是名为的,这是正确的行为指南,下面来自Google documentation
当您的活动在之前被销毁后重新创建时,您可以从系统传递给您的活动的捆绑包中恢复保存的状态。onCreate()和onRestoreInstanceState()回调方法都接收包含实例状态信息的相同Bundle。
由于无论系统正在创建活动的新实例还是重新创建以前的实例,都会调用onCreate()方法,因此在尝试读取状态包之前,必须检查状态包是否为空。如果为null,则系统将创建该活动的新实例,而不是恢复已销毁的前一个实例。
但是,如果处理得当,这并不重要,例如:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
} else {
// Probably initialize members with default values for a new instance
}
...
}发布于 2016-07-15 20:05:27
public void onBackPressed() {
Intent intent=new Intent(C.this, A.class);
// remove below Flag and while going from A dont call finish();
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}https://stackoverflow.com/questions/38395621
复制相似问题