0

In hierarchical navigation, the user navigates up by pressing the Up button on the left side of the toolbar. As of Jelly Bean, there is a easy way to achieve this functionality. Enable hierarchical navigation by adding a parentActivityName attribute in the AndroidManifest.xml file.

When the user navigates up from ChildActivity, an intent like the following is created:

Intent intent = new Intent(this, ParentActivity.class);
startActivity(intent);
finish();

I want to know what exactly happens behind the scene, especially what the intent contains?

Moreover, when I want to preserve the parent activity's state. I know that it can use addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP). Like Android: navigating up to the parent activity recreates the parent activity The first answer says that the intent contains Intent.FLAG_ACTIVITY_CLEAR_TOP . The standard behaviour of this flag is to finish all Activities that are on top of the parent Activity in the task stack including the parent Activity itself and then launch a new instance of the parent Activity.

But when I add the following code in the ChildActivity, running it in the device prior API 23 ,the ParentActivity will not be destroyed and the system just resume the old parent activity. While running it in the device with API 23, the ParentActivity will be destroyed and the system instantiate a new ParentActivity. But in both cases the ParentActivity will preserve its state. What is going on?

public Intent getParentActivityIntent() {
    return super.getParentActivityIntent()
            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}

and where I can find the source code that achieve the UP function! I had google search, but could't find the answer I need! Thanks in advance!

Community
  • 1
  • 1
ZWHmepsy
  • 21
  • 4
  • use official docs https://developer.android.com/training/implementing-navigation/ancestral.html – Viktor Yakunin Jun 07 '16 at 09:15
  • the way you trying to make it work is not standard, that is misunderstanding of the navigation pattern – Viktor Yakunin Jun 07 '16 at 09:17
  • All you need is to add parentActivityName to manifest and this should work for you out of the box. For detailed example you can use this project https://github.com/BukT0p/ActivityTaskBuilder – Viktor Yakunin Jun 07 '16 at 09:24
  • @Viktor Yakunin My previous description made you misunderstand my question. I know we just need to add parentActivityName to manifest. The intent is use to describe the detail. – ZWHmepsy Jun 07 '16 at 09:41
  • ok, so SupportLib has couple methods NavUtils.navigateUpFromSameTask and NavUtils.navigateUpTo this could be useful if you want to find out what happens under the hood – Viktor Yakunin Jun 07 '16 at 10:32

0 Answers0