I wanted to understand when an activity can be in the paused state, so onPause is called, without going into the stopped state (onStop) after. Pretty much like onPause() -> onResume() without passing by onStop -> onRestart -> onStart
onResume method is called when the activity is in focus meaning the user can interact with the visible activity on the screen. onPause is called when the activity loses focus i.e. the user cannot interact with the activity but the activity is still partially visible to the user. onPause and onResume can be seen in action when interacting with a dialog box.
onResume to onPause
When a dialog box appears in front of the ongoing activity, the activity loses focus because the dialog has to gain the user's focus. The activity lifecycle goes from onResume to onPause state. When the user has responded to the dialog and the dialog dismisses then the activity again gains its focus. This time the activity lifecycle goes from onPause to onResume.
onStop is called after onPause only when the android OS detects that the ongoing activity has a chance to get destroyed. This usually happens when activity goes in the background (in recent apps stack). Android system automatically destroys apps from the recent apps stack to free up memory space if those apps are not opened by the user for a while.
Related
In iOS if user press home button, app will move to suspended state. Double click of the home button, suspended apps appear, and selecting an app will bring back it to active state.
How is it by Android? App can become to suspended state pressing center button? Or app move to not running state?
If suspended state exist, how to bring back to running state, where I can select one app among many?
In an activity the oncreate method will get called only once, when app first starts? Which method is get called that will called always when app appears, come back to running state?
Have a look over Android Activity Lifecycle
In android, if the app is not "on the main screen" it counts as Paused state.
By starting an app, there will be a one and only call for onCreate, later will be called onStart and onResume. If you will now click the home button, the onPause and onStop method will be called. Once you wish to back to the activity you can press the Overview button, and select your app from the list and the method onRestart, onStart and onResume will then be called.
If your app is not fully hidden, e.g you swing your finger from top to bottom to see "notification center", this will cause skipping the onStart, onStop and onRestart functions.
In the Android developer diagram, I saw that onResume() is always called before onPause(). Assuming the user starts a new Activity, why should onPause() be preceded by onResume()?
I mean:
OnResume can be called in 2 occassions:
1) when user starting new activity (before OnPause)
2) when activity is in background and if the user brings the activity back to the
foreground
I expect in every case, something else should be done.
You are getting it wrong. Whenever an activity is created, onResume is called just after onStart. Whenever your activity goes back stack onPause is called. Again if your activity comes back to foreground then onResume is called. So, it is not like, onResume is called before onPause. Whenever activity is returning from onPause state, onResume gets called instead of onStart or onCreate. This happens so that Android does not have to create Activity instance again and again though those instances are not properly destroyed. This is quite memory efficient too.
NOTE: If you app is running and the user presses back button or home button, then the Activity goes through onPause() and onStop() state. After this if the user is again coming back to your app then, onRestart(), onStart() and onResume() will be called sequentially.
Then when the activity is only in onPause() state ? When a dialog surfaces on top of your activity or your activity is getting displayed in Split screen but it doesn't have focus (user is not interacting with your app). On these cases, activity goes to onPause() state only.
onResume() is always called before onPause()
This is correct. onResume is always called when the Activity is launched for the first time, before onCreate, and when the Activity is being resumed (user navigates back to your Activity)
Assuming the user starts a new Activity, why should onPause() be
preceded by onResume()
onPause is only called when the Activity is put to background, or before onDestroy if the Application is being destroyed. So onPause is always being called after a call to onResume has been made. Why? Because that's the lifecycle of the Activity as defined by the Android framework.
The life cycle of the activity is as follows
Fresh start via onCreate(), onStart(), onResume .... and close via onPause()->onStop()->onDestroy()
Yellow background: Activity goes into background and thus is no longer visible. The user returns back to the activity.
e.g.
Switch off the phone screen while the activity is running: onPause()->onStop()
Switch on the screen again: onStart() -> onResume()
Green background: The activity stays in the visible screen area but is not active
e.g. Activate multiple windows (split screen) occupying one part of the screen each and tip on your app to make it active
tip on the other app: onPause() is called in your app as it goes into pause but is still visible
tip on your app: onResume() is called
Here is an example of a split screen with two apps:
see android documentation on activity life cycle for details
I have requirement wherein I have to display a lock screen if the user moves out of the application.
Hence, the structure is: Activity A extends Activity B.
Wherein Activity B is the deciding activity : "was application in backgound".
If so it launches the lock activity.
Now, say I am on activity A and receive a phone call. Hence the app gets into the background.
When it resumes I can see the glimpse of Activity A for a fraction of second and then comes the lock activity.
Can there be any solution to avoid that glimpse of Activity A?
You can see the lifecycle of activity from official doc
You are using activity B just to track whether activity is alive. I am not sure if it is necesary.
If activity goes to background onPause() method is called, it means activity is not visible (it might be both screen lock or home button pressed), and onResume() is called when activity is visible again. In Activity A if you override onPause method and launch your lock activity, it should work. (Or set a boolean onPause and launch lock activity on resume(you might see Activiy A though)
Good luck
You're on an activity and you press the home button.
Then you long press home menu button, and select the activity you were on, from the 'recent activities' screen.
What method is called when the activity shows again? onResume, onRestart or any other?
I believe onResume will be called anyways even after pause or stopped.
onRestart may be called if activity has been stopped in the background
The recommendation is to save your data in onPause and rebuild it on onResume with some flags, so flags can tell you if onResume called after onPause/onStopped or Activity is freshly created.
Taken from the Android developer website
"... When the user leaves your activity, the system calls onStop() to stop
the activity (1). If the user returns while the activity is stopped,
the system calls onRestart() (2), quickly followed by onStart() (3)
and onResume() (4). Notice that no matter what scenario causes the
activity to stop, the system always calls onPause() before calling
onStop()..."
Here is the Activity
So no Matter what onResume() would get eventually called.
You can download the ActivityDemo which exhibits the Android lifecycle accurately. This should help you.
I have an activity that is themed as a dialog. I have seen that if the dialog is showing, and then I press the home button, and then using the task manager, restart the app, that dialog activity will be the activity that the app starts in, with no other activities available to go back to. That is, the activity that was running when I loaded the dialog activity is not running. So I just have this dialog-themed activity hovering over the desktop. That makes sense.
Looking over the Android activity lifecycle, the OS does remember the last activity and attempts to restart there. So I created all of the on* methods in my activity (onResume, onRestart, etc). What I found was really puzzling. When I restart the app from the task manager, the following methods are called:
onCreate()
onResume()
onStop()
onDestroy()
Where I was really just expecting
onRestart()
onCreate()
onResume()
Why are onStop and onDestroy getting called right away? And why does the dialog still show, even though onDestroy is called?
How can I configure this app so that it never starts solely on this dialog? I would be fine with the app restarting with the same "parent" activity and the dialog above it (that is, just as I left it), or with just the parent activity running and the dialog dismissed.
In this case, you should use a call to finish() in your Dialog code. You want to do this when the user transitions away from your app (which can happen when they go to the home button, they get a call, etc...). In this case, you would want to make a call to finish() in the onStop() of the Dialog. Calls to finish the current activity remove it from the stack, getting you essentially the behavior you describe.