Let's say I initialize a bunch of stuff inside onCreate
and then bring the app into the background for an hour. What happens if Android frees stuff that was initialized inside onCreate
when I continue using the app? Will onCreate
be called again? I'm asking because I consider to move some stuff inside onResume
instead of onCreate
, which is non necessary in case onCreate
is called again.
What happens if Android frees stuff that was initialized inside onCreate when I continue using the app?
In general, Android does not do that. Android may terminate your process, in which case everything is gone and you will start over from scratch on the next app launch.
However, if your process manages to survive for the hour in the background, most everything will be right where you left it. Some libraries might specifically try to release some memory when your app moves to the background (e.g., caches in an image-loading library), to try to reduce the likelihood that your process will be terminated to free up system RAM.
The documentation has a bit of material on the process lifecycle.
Will onCreate be called again?
If Android terminated your process, then yes, onCreate()
of any started activity will be called again. If your process survived, the activity is already created, so it will not get onCreate()
called again. onStart()
and onResume()
would be called in either case.
The documentation has some material on the activity lifecycle.