Reputation: 690
I am making a demo to understand back stack and activity life-cycle.
I made:
I launched this app and Main Activity is shown. Then press a button to show the dialog, then MainActivityDialog is opened. Then I again press button on MainActivityDialog then BActivity is opened. Finally, I pressed the back button.
MainActivity -> MainActivityDialog -> BActivity ---Back---> MainActivityDialog
Here is the log of this app :
My question are:
Why MainActivity get stopped after launching BActivity from MainActivityDialog? Then after BActivity lifecycle method is called, why MainActivityDialog get stopped?
After pressing back button in BActivity, MainActivity starts first then MainActivityDialog starts and then MainActivityDialog resume?
Upvotes: 0
Views: 76
Reputation: 95578
The order of calls to onStop()
and onDestroy()
on multiple activities is indeterminate.
If you have multiple activities in your activity stack that are no longer visible on screen, Android may call onStop()
on them whenever it wants to and in whatever order it wants to. This is only an indication to the activity that it is no longer visible to the user. You cannot rely on the order of onStop()
calls to multiple activities.
The same goes for onDestroy()
. Android may call onDestroy()
on an activity once that activity has finished. If you have multiple finished activities in your task, Android may call onDestroy()
on them whenever it wants to and in whatever order it wants to. This is also indeterminate. The call to onDestroy()
is just to inform the activity that it is no longer active and that it should release any resources it may have.
There is no guarantee that onStop()
or onDestroy()
will ever be called. The last lifecycle call that is guaranteed is onPause()
. After that, Android can just kill the process without calling any further lifecycle methods.
In your second question you want to know why, after the user presses the BACK button on BActivity
, MainActivity
starts first followed by MainActivityDialog
. The reason is that MainActivity
is visible on screen FIRST and then MainActivityDialog
is visible on screen on top of MainActivity
(because MainActivityDialog
is Dialog-themed, it doesn't cover the entire screen and you can see parts of MainActivity
underneath it).
Upvotes: 1
Reputation: 10948
Im not a pro at Android, but since nobody has answered yet, i will try my best. I want to help because i also learn android by doing something like you did (using log cat to see activiti's current state) for the first time.
Why MainActivity get stopped after launching BActivity from MainActivityDialog?
Every time you start a new activity, the old activity will always be paused/stopped.
Then after BActivity lifecycle method is called, why MainActivityDialog get stopped?
Because now the current active activity is BActivity
, in other word : MainActivityDialog
is not active/visible hence its stopped/paused.
After pressing back button in BActivity, MainActivity starts first then MainActivityDialog starts and then MainActivityDialog resume?
Yes, because you started MainActivityDialog
from MainActivity
, so MainActivity
will be restarted first.
Please feel free to comment, im also still learning :)
Upvotes: 0