Reputation: 7959
Imagine the app has 2 activities, A and B. From A, you push a button that starts B.
From B, you push another button that runs setResult(RESULT_CANCEL, new Intent()); finish();
This way, the view pops back to A.
I'm testing this on two devices, one running Gingerbread (GB), the other running Jellybean (JB).
On GB, when I return form B to A, the method the method onCreate
is not called, which I believe means the activity has not been deallocated (onDestroy
was not called either).
However on JB, the method onCreate
is called again, and I don't understand why.
The big issue is that in my app, the onCreate
is used to load data from the internet. This way, JB will always reload that data, even when it's been downloaded already.
What exactly is happening here, why do they behave differently?
Upvotes: 2
Views: 2203
Reputation: 4284
The framework will call onCreate()
when it needs to create a new instance of your Activity, which is likely what is happening here.
It is perfectly acceptable for the framework to destroy your Activity after you've navigated away from it (onStop()
), and re-create it when you come back. There are no guarantees about when this will happen.
Ordinarily, this doesn't happen until there is memory pressure, but this varies from device to device (and of course depends on the operating load). You can force this behavior to happen immediately by enabling "Don't Keep Activities" in Settings -> Developer Options
.
Finally, you can also use this command to look at the activity hierarchy directly: adb shell dumpsys activity activities
. It takes some experience to decode everything, but should give you a rough idea of the Activity Manager's view of the world at any point in time.
Upvotes: 6