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"
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).
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.
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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With