锁定手机屏幕后,活动再次开始

时间:2015-10-20 14:20:27

标签: android android-activity

我有下一个问题。我正在开发一款游戏。当我从物理按钮锁定设备并解锁时,游戏再次启动。活动再次开始。当我解锁它时,我想从锁定它的那一刻起继续玩。

2 个答案:

答案 0 :(得分:0)

然后你需要在onPause中保存状态并在onResume

中再次加载它

答案 1 :(得分:0)

您需要save and restore state of your activity使用onSaveInstanceStateonRestoreInstanceState

enter image description here

save:

static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
...

@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);
}

restore:

public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);

    // Restore state members from saved instance
    mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
    mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}