0

I'm followed https://developer.android.com/training/appbar/up-action.html to add an up button for the app bar.

my manifest is

     <activity
        android:name=".activity.MyDiaryAct"
        android:label="@string/title_activity_my_diary"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".activity.PageListAct"
        android:label="@string/title_page_list"
        android:theme="@style/AppTheme.NoActionBar"
        android:parentActivityName=".activity.MyDiaryAct">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="activity.MyDiaryAct" />
    </activity>
    <activity
        android:name=".activity.PageAct"
        android:label="@string/title_page_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar"
        android:parentActivityName=".activity.PageListAct" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="activity.PageListAct" />
    </activity>

I started PageAct from MyDiaryAct and click up button. It returned to myDiaryAct but I'm expecting PageListAct.

My codes at PageAct

    android.support.v7.app.ActionBar ab = getSupportActionBar();
    ab.setTitle(page.getName());
    ab.setDisplayHomeAsUpEnabled(true);

What am I missing? I wish not to add codes at onOptionsItemSelected(). Thanks!

PS: I'm using targetSdkVersion 23.

UPDATE: while the possible duplicate post provides useful information and direction to solve my problem but the complete understanding and solution found at here. https://developer.android.com/training/implementing-navigation/ancestral.html

In short, the parent has to be in backs tack else I need to create it manually.

hjchin
  • 864
  • 2
  • 8
  • 25
  • Actually parent activity has to be in the activity stack, in other words your PageListAct activity has to be created before PageAct. Up button does nothing but pop your activity stack until that parent activity shows up. – Binary Baba Nov 04 '16 at 15:29
  • Can you point me to document that stated this? – hjchin Nov 04 '16 at 15:31
  • It says in the doc itself, check the comments "A child of the main activity" – Binary Baba Nov 04 '16 at 15:33
  • I thought parent and child is a hierarchy design concept, not something from the stack. So it is designed for a situation where there is multiple siblings, instead of clicking the back button, click up button back to the parent? – hjchin Nov 04 '16 at 15:48
  • There cannot be multiple siblings, coz its a linear flow. – Binary Baba Nov 04 '16 at 15:56
  • Possible duplicate of [Returning from an activity using navigateUpFromSameTask()](http://stackoverflow.com/questions/14462456/returning-from-an-activity-using-navigateupfromsametask) – X3Btel Nov 04 '16 at 15:59
  • why not? I mean in hierarchy view. In this context, the pageListAct is the listing while PageAct is the individual page, I could go back to previous and next page and go up. For now, i have to finish each page before i move to siblings as it will otherwise stays in the stack. The back button is used to serve as up button here. With this up button, then I could clean my code so that back is really going back to the page before (be it previous or next) and up button is really brings me back to listing. – hjchin Nov 04 '16 at 16:14
  • 1
    See the duplicate one. ParentActivityName only searchs for Parent activity and brings it to top of the stack. If it is missing it just triggers on backPressed. If the activity is present in the backstack and still it does not trigger then there is problem – X3Btel Nov 04 '16 at 16:25
  • Thanks @X3BTel, I will try it out. – hjchin Nov 04 '16 at 16:32
  • Thanks both @X3BTel, I removed the finish() at PageAct and tried: MyDiaryAct -> PageListAct -> PageAct (page 1) -> PageAct (page 2) -> PageAct (page 3) and then up, Yes, it brings me back to PageListAct. I found this https://developer.android.com/training/implementing-navigation/ancestral.html explained on the hidden logic (the manual way) The duplicate post touches on additional logic if I wanted to implement it manually. I think I would just keep the UI code simple and follow what has been offered. – hjchin Nov 05 '16 at 03:05

0 Answers0