Will onCreate called after onStop without onDestroy in the middle?
From the picture below, the answer is YES.
But in the same page , it says:
protected void onStop()
Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.
My app need to run a background task even if the user leave to another app or turn off the screen. The only situation where the task should be stopped is that the app is entirely closed. So I create and run the background thread in onCreate()
and stop it in onDestroy()
. The problem I am facing is that sometimes there are more than one of my threads running at the same time. This often happens when I leave my app for a long time then back. I think it may due to multiple calls of onCreate()
. How to solve it?
Will onCreate called after onStop without onDestroy in the middle?
Short answer: No
Long answer:
onCreate
won't be called if just onStop
has been called on an activity
. After onStop
if user navigates back to activity, it's onStart
(before that onRestart
will also be called) will be called.
Now let me explain how onCreate
will be called after onStop
without onDestroy
being called.
For that you need to look at this process priority hierarchy in android:
Activity remains in foreground processes from the time onResume
has been called but onPause
hasn't been called. And since foreground processes has highest priority, it won't get killed.
onPause
is called when user is no longer able to interact with the activity
but it is VISIBLE . Example: when you open a DialogFragment
above the activity. Then activity process moves into visible process . This has lesser priority than foreground process but has enough priority to not get killed.
Not after that comes onStop
, which is called when user is no longer able to see the activity that means activity is not visible . That comes the point when activity process moves into background processes. Now it can be killed if system need more memory and your activity is consuming more memory than others (this is off-topic of this answer that which background is first to get killed).
So if an activity which is in background and is killed by the system, onDestroy
is not guaranteed to be called . And in that case, onCreate
will be called after onStop
without onDestroy
being called.
That's why in the question image (activity lifecycle), it is shown that onCreate
will be called after onStop
if App process is killed .
Edit 1:
If you want to handle anything (like freeing up memory, killing memory intensive tasks, etc) before activity being destroyed and since onDestroy
is not guaranteed to be called, you can implement ComponentCallbacks2
(only for api level >= 14) to receive onTrimMemory
callback with varies stages of your application/activity. Varies stages and their explanations are here .
If you want to implement the same in API level < 14, you can use onLowMemory
callback which is roughly equivalent to TRIM_MEMORY_COMPLETE
.
This often happens when I leave my app for a long time then back. I think it may due to multiple calls of onCreate(). How to solve it?
In onCreate()
you can check if your background thread is running, if yes then don't start it again.
More importantly, is your background thread a service
or AsyncTask
or just simple Thread
?
Considering the wording in your question i would recommend you to go for service
if you are not already using it. It won't create "multiple thread" problem.
Please use Service
to run a background task. If you want to stop the service when current activity destroys, you can use Bind Service.
Please Post your code snippet to get exact idea, how are you trying. we can help more
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.