【发布时间】:2015-10-20 22:29:49
【问题描述】:
我遇到了下一个问题。我正在开发一个游戏。当我从物理按钮锁定设备并解锁时,游戏又开始了。活动再次开始。当我解锁它时,我想从锁定它的那一刻起继续玩。
【问题讨论】:
我遇到了下一个问题。我正在开发一个游戏。当我从物理按钮锁定设备并解锁时,游戏又开始了。活动再次开始。当我解锁它时,我想从锁定它的那一刻起继续玩。
【问题讨论】:
然后需要在onPause中保存状态,在onResume中再次加载
【讨论】:
你需要save and restore state of your activity 使用onSaveInstanceState 和onRestoreInstanceState
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); }
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); }
【讨论】: