سؤال

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;
} 
هل كانت مفيدة؟

المحلول

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.

Activity Lifecycle

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top