所以,我有以下后会失去状态: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