所以,我有以下後會失去狀態:Android應用程序啓動另一個意圖
一個通用類,很多在我的Android應用我的活動的機會,通過我的清單設置類:
<application android:name="com.dev.games.phraseparty.Common"... />
現在,在這個類中,我有幾個對象被構造來保存應用程序狀態和公共服務,以執行在通用構造函數中構造的應用程序
即
GameStateVO gameState;
public Common()
{
gameState = new GameStateVO();
}
我的問題是我的Activity有一個Admob廣告。當用戶點擊廣告時,它會調用webbrowser意圖在網頁瀏覽器中打開廣告網址。
現在,當我從admob發起的webbrowser點擊回來時,它將我帶回到校準活動並調用OnCreate方法。
這個活動然後得到一個空指針異常,因爲它會做這樣的事情:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Common common = this.getApplication();
//null pointer here since the game state VO is null since the Common has lost its state.
int score = common.getGameState().getScore();
}
這聽起來像你的應用程序正在經歷一個艱難的殺戮。你可能想要堅持國家在onStop的硬性殺死說偏見。你可以通過使用一個標誌isSavedInstanceState來做到這一點。在onResume中將其設置爲false,並在onSaveInstanceState中將其設置爲true。然後在onStop中寫入prefs if(!isSavedInstanceState)。如果onCreate == null中的包,則從onCreate中的prefs讀入。 – JAL 2011-05-17 06:17:20