4

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;
} 
Kajuna
  • 449
  • 5
  • 20
  • thats interesting, that onCreate is called again from the homeScreen... onDestroy will only be called when resources are low, but onPause, onStop are your better bets. Can you do your coding in onStop or onPause or does it have to be in onDestroy() – reidisaki Feb 27 '14 at 20:33
  • Sorry, onStop is a processing method, I believe, I meant onPause, there. I will edit that. The thing is I do not want to destroy my activity or create it again, I just want to pause and then resume it (which is the normal behaviour, I believe) – Kajuna Feb 27 '14 at 21:31
  • It is not normal behavior at all. The activity is recreated all the time, even after a screen rotation. It is your job to store the state of the activity by overriding `onSaveInstanceState()` method and then restoring it again in `onCreate()`. The activity still stays in memory though, so the Activity instance survives much longer. – Floris Feb 27 '14 at 21:43
  • Do you have the "Don't keep Activities" developer option turned on? – devunwired Feb 27 '14 at 21:50
  • No, @Devunwired, and this happens on every device. Not just mine :S – Kajuna Feb 28 '14 at 02:14

4 Answers4

5

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.

Kajuna
  • 449
  • 5
  • 20
  • This fix is legit. If you have an app that uses and requires a lot of memory, any time that app is placed in the background it is liable to be closed. In this instance, you will run in the issue that the poster posted. The only solution is this one provided by Kajuna. Thank you! – portfoliobuilder Mar 04 '16 at 02:46
3

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.

marcinj
  • 48,511
  • 9
  • 79
  • 100
1

Activity Lifecycle

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

Mahdi-bagvand
  • 1,396
  • 19
  • 40
  • Ok. So following the diagram, you can only go from onStop() to onCreate() by killing the process. Or otherwise onRestart() would be called. Both options would be fine for me. The problem I (think) am facing is that onCreate() is being called without the Process being killed first. Does that make sense? – Kajuna Feb 28 '14 at 00:57
0

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.

sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86