Reputation: 34497
I have one confusion that when system call onPause
and onStop
method of activity lifecycyle. I see that by navigating to another app from my activity, here system is calling onPause
and onStop
and when I start new another activity of my application again system is calling onPause
and onStop
method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(this, SecondActivity.class));
}
Here this answer says "onPause() call when one Activity transfer control to another Activity, and onStop() method call when Activity in finish mode." that seems wrong to me that when I am starting another activity both are calling and I simply start the activity and did not finish it but still onPause
and onStop
method is calling.
Doc says "When your activity is not longer then it call
onStop
" Here I am OK it callsonStop
Confusion when your activity is no longer visible and hidden by another activity but still onPause
is also calling but why ??
Upvotes: 0
Views: 1315
Reputation: 2581
To keep it simple, here is the small info from the android developers documentation.
Activity Lifecycle
Activities in the system are managed as an activity stack. When a new activity is
started, it is placed on the top of the stack and becomes the running activity --
the previous activity always remains below it in the stack, and will not come to the
foreground again until the new activity exits.
An activity has essentially four states:
If an activity in the foreground of the screen (at the top of the stack), it is active or running.
If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.
Please refer to this life cycle demo application. It's really helpful in understanding the Activity lifecycle.
Also as Raghunandan suggested, you must read this.
Added:
For instance: When a user traverses from Activity A to Activity B (FullScreen Non Transparent) following things happens
onPause is guaranteed to be called on Activity A (No matter whether Activity B is non-full-sized transparent or full-sized). onStop will only be called when Activity A is completely overridden by full-sized Activity B.
So in onPause you can save an Activity's state or some other useful info if required.
Good Luck :)
Upvotes: 1