onCreate() called while Activity is stopped (but not destroyed). Only after installation

半腔热情 提交于 2019-11-30 10:17:13

Ok, this is how I solved it, in case anyone else bumps into this wall.

This might affect especially people coming from the Processing Developing Environment, as it converts a "Processing sketch" into the only activity of the project.

The original problem (Android managing apps in a different -wrong?- way when launching them from the Package Installer) is well explained here: https://code.google.com/p/android/issues/detail?id=26658

Solutions posted there might solve most cases,but if -like me- your launcher activity is the one that carries out all the work, you will need to create a specific launcher activity that just starts the main one... and commits suicide when the Android bug happens.

Add this bit to the onCreate() method of the launcher activity:

if (!isTaskRoot()) {
 finish();
 return;
} 

I hope this helps.

This looks like a valid application lifecycle, you put your app to background, android is then allowed to destroy your app. onDestroy is not guaranteed to be called, you have to be prepared for that, as you say onPause is called so you can use it. Why it happens only once after install is hard to explain, but in my opinion you should not actually care about it and prepare your app to be killed any time it is in background.

In above diagram may OnDestroy method never called because other application need memory.

onStop() is the last method which is guarantied to be called. After this method Android is allowed to kill your activity at any time. Check out the table in the activity's lifecycle table. There is a "Killable" column describing the time, when activity can be killed.

If you don't use static variables and you initialize everything properly in onCreate(), then you should have no issues with this behavior. Just never expect onDestroy() to be called.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!