2015-10-20 98 views
0

我遇到了下一个问题。我正在开发一款游戏。当我从物理按钮锁定设备并解锁它时,游戏再次开始。活动再次开始。当我解锁它时,我想从锁定它的那一刻开始继续播放。锁定手机屏幕时活动再次开始

回答

0

然后,你需要保存的onPause状态和的onResume

+0

是的,但是在我的活动我有一个100个变量,必须有一种简单的方法 – user3240604

0

再次装入您使用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); 
} 
需要 save and restore state of your activity

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); 
} 
+0

是的,但是在我的活动我有一个100个变量,必须有一个简单的方法 – user3240604

+0

你可以尝试在每次更改时将变量存储到共享首选项。即使这样也不容易。到现在为止,我想不出任何其他方式。实例状态是推荐的状态。 –

+0

另外,你需要仔细选择哪些变量来保存,如果你仔细看看,你可能会发现你不需要保留所有的变量 –