重新打开时,防止由旧完成导致的活动关闭

时间:2013-01-22 14:22:31

标签: android android-intent android-activity android-lifecycle

我有两个活动A和B. 在A中有一个按钮BTN:

Intent myIntent = new Intent(A.this, B.class);
startActivityForResult(myIntent, B_VIEW);
  1. 我点击BTN
  2. 然后我点击在B中执行完成()的后退按钮。
  3. 然后我快速按下再次打开B的按钮BTN。
  4. 问题在于,如果B.onDestroy()由先前的finish()(步骤2)引起,尚未执行,则会立即执行,因此B关闭: - (

    我想要那个,如果还没有执行,如果我重新打开B,那么B.finish()不会开火吗?

1 个答案:

答案 0 :(得分:0)

你最好重新开始处理这种过程。

最好的办法是将密钥数据打包到onSaveInstanceState中的捆绑包中,然后检查onCreate(Bundle)函数中是否存在该捆绑包。这样的事情可以起作用(Largely copied from the Android Docs)

@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);
    }
}
相关问题