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

核能气质少年 提交于 2019-11-29 15:15:47

问题


I'm working on an app that targets Api 19, and that is basically a Processing sketch.

The problem I'm facing is that the first time my app is run, right after installing it, it works well until the user sends it to the background. Then, if they click on the app icon again, onCreate() is called but the activity is not destroyed or restarted. Some variables change and that produces strange behaviours.

This happens ONLY the first time the app is used. After force closing it, this behaviour won't happen ever again (as far as I've tested). And this does not happen when launching the app from Eclipse either.

Summarising, this is what happens after the first force close (and what I deem correct):

  • Activity is running.

  • Activity is sent to back via home button

  • onPause()

  • We click on the app icon again

  • onResume()

And this is what happens -ONLY- the first time the app is run after installation:

  • Activity is running.

  • Activity is sent to back via home button

  • onPause()

  • We click on the app icon again

  • onCreate() <-- !! note no onDestroy()

  • onResume()

I wonder if the fact that I'm using Immersive Mode has anything to do with this, but changing the Api target version to 10, removing Immersive mode or testing on old devices do not help. I have, of course, used android:configChanges="orientation|keyboardHidden|screenSize" on my manifest.

Does anyone have a clue on what might be causing this? Is this a common issue or should I look for a bug in my code? Maybe a Processing bug?

Thanks in advance for any clue. I hope this is the right way to ask about this issue. This is my first post.

Update: My explanation is not very accurate, but apparently there is a bug report for this. The problem is much better explained here: https://code.google.com/p/android/issues/detail?id=26658

Unfortunately, I can't get the proposed solutions to work, using this in onCreate() causes my app to either close or crash:

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

回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/22079232/oncreate-called-while-activity-is-stopped-but-not-destroyed-only-after-inst

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