2015-10-20 99 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

另外,你需要仔細選擇哪些變量來保存,如果你仔細看看,你可能會發現你不需要保留所有的變量 –