Silly question about android activty lifecycle
-
27-10-2019 - |
Question
I have activity+service classes.
When program runs activity will be shown and service will be started in onCreate
method of activity.
When I clicked on HOME button of virtual machine, HOME SCREEN will be appear but activity will have been gone
- at this point, I wonder that program in which situation -paused/stopped- ???
- what happened to the service? (is it still running?)
- how can I bring the activity to front using the service?
Thank you...
Solution
Well in your case for the activity if you press the home button it is paused. and by launching it again with pressing the home button it just resumes it.
for the service: it depends how your service is started..
if you start the service with startService(intent) then your service is running until:
a) you explicitly stop it by calling stopService.
b) android kills it
c) you call selfStop from inside the service
if you start the service with bindService() without calling startService() then the service will:
a) run untill the activity is destroyed (not paused, so this means it will keep running if you press the home button) and this also means you need to call unbind().
b) android kills it
c) untill you call selfStop from inside the service
to bring the activity to foreground you need to call an intent and set the flag i believe it was launch from history or something like that.... you will have to check the docs.
intent.addFlag(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); <- i think or this one -> FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
OTHER TIPS
See below the activity flow, hope it helps
The state of your Activity
(and Service
) could vary depending on what else is happening. If the Android system needed resources it may have destroyed your Activity
(and possibly also your Service
) to obtain them. You should read up on:
- The Activity Lifecycle and
- The Service Lifecycle
In the Activity
diagram (also in Mark Bakker's answer) pay particular attention to the box containing the text 'Other applications need memory'.