Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must I specify the parent activity name in the Android Manifest?

Tags:

I have an activity that starts another activity.
Is it mandatory that I specify the parent activity in the Android Manifest? Im asking this because there might be other activities that will start this one as well, so should I specify all of them?

android:parentActivityName="com.example.myfirstapp.MainActivity"  
like image 297
Kaloyan Roussev Avatar asked Oct 06 '13 10:10

Kaloyan Roussev


People also ask

How do I set parent activity on Android?

Declare a Parent Activity To support the up functionality in an activity, you need to declare the activity's parent. You can do this in the app manifest, by setting an android:parentActivityName attribute. The android:parentActivityName attribute was introduced in Android 4.1 (API level 16).

How do I register activity in manifest?

Goto your Android Manifest , goto the Applications Tab (at the bottom), click on "Add", Choose Activity. On the right, next to Name: Click on Browse , to get a list of available activities, just add it and you're set! :) You could right way just edit the Manifest XML aswell. It's upto you.

What is the activity manifest Android?

The Android Manifest is an XML file which contains important metadata about the Android app. This includes the package name, activity names, main activity (the entry point to the app), Android version support, hardware features support, permissions, and other configurations.


2 Answers

As per docs -> section android:parentActivityName:

The system reads this attribute to determine which activity should be started when the user presses the Up button in the action bar. The system can also use this information to synthesize a back stack of activities with TaskStackBuilder.

So you only need to specify that if you're going to use up-navigation (as opposed to navigation by back button) or TaskStackBuilder. In other cases, you don't need it.

For more information, see the up-navigation docs. (Archived from the original, which now redirects to the Jetpack Navigation docs on designing navigation graphs)

like image 106
Szymon Avatar answered Nov 02 '22 04:11

Szymon


While it should be defined if upward navigation or backstack synthesis is desired, note that the attribute android:parentActivityName was introduced in API Level 16.

For prior releases, parent activity information is accessed from attributes defined inside of a <meta-data> tag that is declared inside of the child <activity> tag.

Example:

    <activity         android:name=".DetailActivity"         android:parentActivityName=".MainActivity">         <meta-data             android:name="android.support.PARENT_ACTIVITY"             android:value=".MainActivity"/>     </activity> 

Inside of the <meta-data> tag, set the android:name attribute to android.support.PARENT_ACTIVITY, and the android:value attribute to the parent activity class name (i.e. the same class name as that assigned to android:parentActivityName).

Unless API level is known, both the <meta-data> and inline specifications are recommended.

For more on specifying the parent activity, see: https://developer.android.com/training/implementing-navigation/ancestral.html#SpecifyParent

In addition, consider defining the android:launchMode attribute inside of your main <activity> tag to set the desired behavior of upward navigation: https://developer.android.com/guide/topics/manifest/activity-element.html#lmode

like image 21
Robert John Avatar answered Nov 02 '22 06:11

Robert John