Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
} 
like image 320
Kajuna Avatar asked Jan 12 '23 03:01

Kajuna


1 Answers

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.

like image 73
Kajuna Avatar answered Jan 13 '23 17:01

Kajuna