Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to set parent activity for new activities in Android Studio. Error: Hierarchical parent must already exist

I am starting to build my first app from developer.android.com. Recently switched to Android Studio and found that I can't set the hierarchical parent for the new activity (DisplayMessageActivity). It states "Hierarchical Parent must already exist". Can someone please guide me on this? In Eclipse, it works fine.

like image 240
dknchris Avatar asked Mar 09 '14 04:03

dknchris


People also ask

How do I set parent activity on Android?

Declare a Parent Activity 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). To support devices with older versions of Android, define a <meta-data> name-value pair, where the name is "android.

What does finish () do in Android?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

How do I get back the result from child activity to parent in android?

Intent data = new Intent(); data. putExtra("myData1", "Data 1 value"); data. putExtra("myData2", "Data 2 value"); // Activity finished ok, return the data setResult(RESULT_OK, data); finish();

Can we create activity without UI in Android?

Explanation. Generally, every activity is having its UI(Layout). But if a developer wants to create an activity without UI, he can do it.


1 Answers

I was also doing the My First App tutorial on the Android Developer website using Android Studio and experienced this same problem. Thanks to charmarel for the tip that Android Studio will allow you to leave the problematic field blank. However, this will cause you do some of the legwork that the IDE would otherwise do for you in order to have the app properly running.

The workaround is not all that bad:

  1. As mentioned, Android Studio (0.5.2) will allow you to leave the Hierarchical Parent field blank so just fill out the Activity Name, Layout Name, and Title as you normally would and ignore the Parent.
  2. Now about that legwork that is no longer being done for you... You will need to edit the AndroidManifest.xml file yourself to define the Parent Activity information for this new Activity as you have just left blank in the New activities dialog window.

Edit the new Activity node to include this information, it should look like this:

<activity
    android:name="com.example.myfirstapp.DisplayMessageActivity"
    android:label="@string/title_activity_display_message"
    android:parentActivityName="com.example.myfirstapp.MainActivity" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.myfirstapp.MainActivity" />
</activity>

That's it, issue circumnavigated!

like image 166
Derek W Avatar answered Sep 27 '22 02:09

Derek W