我有下一个问题。我正在开发一款游戏。当我从物理按钮锁定设备并解锁时,游戏再次启动。活动再次开始。当我解锁它时,我想从锁定它的那一刻起继续玩。
答案 0 :(得分:0)
然后你需要在onPause中保存状态并在onResume
中再次加载它答案 1 :(得分:0)
您需要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); }