4

Android documentation states that regarding the onCreate method:

Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. Always followed by onStart().

However I have a strange behavior:

  • I have an Activity A. When I start my application, onCreate is called. That's normal.
  • I then press the home button. onStop is called. That's normal.
  • I go back to my application, onCreate is NOT called. That's normal.
  • I press home again.
  • I go to another application (dropbox, gmail or whatever) to access a file with an extension that is associated to my application (I have an intent-filter with a pathPattern to this file extension)
  • I select this file to open it to my app. onCreate is called though onDestroy has never been called. Why is that ? If I set a boolean in my activity to check if this is the first time that I call onCreate, this boolean says true twice (the very first time, and the second time when opening the app using the file extension).

I guess I misunderstood something with the onCreate. But the doc doesn't help me. Any idea ?

Vincent
  • 1,013
  • 14
  • 33

2 Answers2

3

there's a very likely chance that it's a different instance of your activity. The share intent is probably launching your activity on the other app (e.g. dropbox) stack. Making it two instances of your activity class running with two separate onCreate

you can check that by pressing the multi-task button on the device and see if you have your application screen-shot preview two times: one with your own app icon, and the other with the other app (e.g. dropbox) app icon.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • 1
    I just checked, but using the multi-task button I see only one instance of my app. – Vincent Jun 04 '14 at 15:05
  • and logcat shows the same pid so it seems that there's only one instance. – Vincent Jun 04 '14 at 15:07
  • 1
    @Vincent Different instance of the activity, not the app. you won't see it in task manager. Use DDMS to check. – Simon Jun 04 '14 at 15:07
  • @Simon yes, you're right. Using this method (http://stackoverflow.com/a/10618496/575481) it appears that I have multiple instance of my activity. I guess dropbox started a new one. How can I avoid this !? – Vincent Jun 04 '14 at 15:19
  • 2
    happy to know I could help. You probably can avoid it by using some flags in the manifest, like singleInstance, task afinity etc. I found this great PDF once (don't ask me where, I Googled it, or G+) and it have really great tips regarding stack: https://drive.google.com/file/d/0B4UthPrlbfJPNG9PUGpWR1JyRDg/edit?usp=sharing – Budius Jun 04 '14 at 15:38
0

The activity in those 2 cases gets different intent. And when launching from other app you are actually in other Task (back stack), activity's default android:launchMode is new instance per task.

see

Paul Verest
  • 60,022
  • 51
  • 208
  • 332