简体   繁体   中英

Method OnCreate being called again when I return from another activity

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(); 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM