0

我有两个活动 A 和 B。A 中有一个按钮 BTN,它可以:

Intent myIntent = new Intent(A.this, B.class);
startActivityForResult(myIntent, B_VIEW);
  1. 我点击 BTN
  2. 然后我单击在 B 中执行 finish() 的后退按钮。
  3. 然后我快速按下按钮 BTN 再次打开 B。

问题是,如果B.onDestroy()由先前finish()(步骤 2)引起的 尚未执行,它现在执行,所以 B 关闭:-(

我希望,如果尚未执行,如果我重新打开 B,B.finish() 将不会触发。如何?

4

1 回答 1

0

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

最好的办法是将关键数据打包到 中的一个包中onSaveInstanceState,然后检查该包是否存在于onCreate(Bundle)函数中。像这样的东西会起作用(大部分是从 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);
    }
}
于 2013-01-22T14:27:47.820 回答