开发者

unable to reopen Android App after this.finish()

开发者 https://www.devze.com 2023-02-24 00:26 出处:网络
My Android App is getting a bit hefty and it runs slow. I haven\'t got round to optimizing yet. I\'m still ironing out some bugs.

My Android App is getting a bit hefty and it runs slow. I haven't got round to optimizing yet. I'm still ironing out some bugs. Just some background: my app is a game, that the user can win or lose (naturally). I havent made a "win sequence" or "lose sequence yet" so I just put the code in

if(userWins())
   this.finish();

where the app exits abruptly. Nothing wrong so far. But, when I try to open the app again I get a blank screen, when I should get option buttons. If I turn off my phone and turn on again I can use the app, but otherwise I can't. I have no idea why this is.

开发者_JAVA百科

Side note: My copy of AngryBirds has a similar problem. If I exit the game screen "improperly" (i.e. while in the middle of a game, not in the options menu) I cannot turn on the app again until I reboot my phone.


When you call finish() Android don't kill the process yet. In case you restart your application, the process will be kept running unless the OS need the ressources. see: http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle

This is why it works when you reboot. (you do not need to reboot, just kill the process using adb shell kill <PID> or a task killer if you aren't root)

Your application is likely leaving some variables initialized with old values, and when you restart your activity you find yourself in an unstable state with variables pointing to the old data and other variables to newly initialized data.

You need to either set the old data to null in the onDestroy() event, either ensure initialisation is done in the onCreate() event. You choose wich one depending on if the data can be reused or not.

Look for singletons, or for this kind of initialisation structures:

if (mVar == null) {
 mVar = "stuff";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消