I'm currently learning the Activity Lifecyle. I noticed the following:
- I have two Activitys, A and B.
- When I open Activity B from Activity A, A gets stopped and B gets created and started.
- When I press the Back Button on my device, B gets destroyed and A get restarted.
- But when I use the Back / Up Botton of the Actionbar instead, B gets destroyed, A gets destroyed and then onCreate() is called.
Why gets A destroyed instead of restarted, when using the Up Botton in the ActionBar?
I hope my question is clear, if not please comment.
When you press the BACK button, this calls
onBackPressed()in the currentActivity. The default behaviour of that method (if not overridden in theActivity) is to callfinish()on theActivity. This finishes theActivityand resumes theActivitywhich is underneath it.The UP button is calling
startActivity()with anIntentthat is built like this:This code will remove all activities in the stack back to, and including,
TargetActivityForUpButton. It then creates a new instance ofTargetActivityForUpButtonand launches thatActvity(you will seeonCreate(),onStart(),onResume()called on theActivity.See also the section "Navigate up to parent activity" in https://developer.android.com/training/implementing-navigation/ancestral