Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build an Interactive Story App (Retired) Intents and Multiple Activities Adding a Second Activity

How can I add a Hierarchical Parent after the Activity is created?

Hi, I'm working on my own app, and just learned about Hierarchical Parents, and would like too add one to one of my activities. However, it has already been created, so I don't know how to get to that creation screen again. How can I add a hierarchical parent after an activity has been created?

2 Answers

Hi Stephanie,

From Android Studio, choose Tools > Android Device Monitor or click the Android Device Monitor icon. Click the Open Perspectives icon, and select Hierarchy View.

There is just a black screen in the Hierarchy View window - what should I do after getting to Hierarchy View?

Sorry, I misunderstood; to change the parent of a given activity, you'd go to AndroidManifest.xml and add the activity as follows;

<activity android:name="CHILD_ACTIVITY" >
    <meta-data
       android:name="android.support.PARENT_ACTIVITY"
       android:value="PARENT_ACTIVITY_PATH" />
</activity>

Substitute PARENT_ACTIVITY_PATH with the name of your parent activity, adding also your package name (e.g. com.example.NameOfParentActivity).

Not to confuse the parent activity with the launch activity;

    <activity
        android:name="com.example.StartActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" /><!-- This -->
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.MainActivity"
        android:label="@string/app_name" >
    </activity>

Where the category.LAUNCHER is determining the launch activity.

Hope that's more helpful!

Sean Wilson
Sean Wilson
3,791 Points

Evan Demaris, is 'android:parentActivityName=".ParentActivityName" ' also required, or optional?

Hi Sean,

This is the example from Android on providing 'up' navigation; it looks to be mandatory for Android 4.1+.

<application ... >
    ...
    <!-- The main/home activity (it has no parent activity) -->
    <activity
        android:name="com.example.myfirstapp.MainActivity" ...>
        ...
    </activity>
    <!-- A child of the main activity -->
    <activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
</application>