的Android已經在做這個給你。說你是在活動答:您開始與活動B:
Intent myIntent = new Intent(this, myAcitivity.class);
startActivity(myIntent);
的onPause()爲當前活動你去myActivity之前將被調用,其中的onCreate()被調用。現在,如果您按回按鈕,myActivity的onPause()會被調用,並且您將返回到調用onResume()的活動A.請閱讀文檔here和here中的活動生命週期。
要保存活動的狀態,你必須覆蓋的onSaveInstanceState()回調方法:
系統調用該方法,當用戶離開你的活動,併爲其傳遞一個將被保存在Bundle對象你的活動被意外銷燬的事件。如果系統稍後必須重新創建活動實例,它將同一個Bundle對象傳遞給onRestoreInstanceState()和onCreate()方法。
例子:
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);
}
有一個在文檔更多相關信息,請有很好的閱讀關於保存/恢復您的活動狀態here。
你能展示這些解決方案中的任何一種嗎? –
請嘗試從Activity B的onPause()方法轉到Activity A –
@PankajKumar,我提到過我試過'moveTaskToBack(true);' –