Android 防止活动在重新打开时因旧完成而关闭
我有两项活动A和B。 a中有一个按钮BTN,用于:Android 防止活动在重新打开时因旧完成而关闭,android,android-intent,android-activity,android-lifecycle,Android,Android Intent,Android Activity,Android Lifecycle,我有两项活动A和B。 a中有一个按钮BTN,用于: Intent myIntent = new Intent(A.this, B.class); startActivityForResult(myIntent, B_VIEW); 我点击BTN 然后我单击后退按钮,在B中执行finish() 然后我快速按下按钮BTN,再次打开B 问题是,如果由上一个finish()(步骤2)引起的B.ondestry(),尚未执行,则现在执行,因此B关闭:-( 我希望,如果尚未执行,如果我重新打开B.How,B
Intent myIntent = new Intent(A.this, B.class);
startActivityForResult(myIntent, B_VIEW);
finish()
(步骤2)引起的B.ondestry()
,尚未执行,则现在执行,因此B关闭:-(
我希望,如果尚未执行,如果我重新打开B.How,B.finish()将不会启动。您最好重新开始处理这种过程 您最好将关键数据打包到
onSaveInstanceState
中的一个捆绑包中,然后检查onCreate(bundle)
函数中是否存在该捆绑包。类似这样的操作可以:
似乎有点不对劲,活动的每个实例的
finish()
都应该是单独的。除非活动B的启动模式设置为singleTop
,否则我想。。。
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
public void onCreate(Bundle savedInstanceState)
{
if (savedInstanceState==null)
{ //This is the first time starting
mCurrentScore=0;
mCurrentLevel=1;
}
else
{
mCurrentScore=savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel=savedInstanceState.getInt(STATE_Level);
}
}