Home » Android » android – Navigating back to parent activity in ActionBar without handling Up button event

android – Navigating back to parent activity in ActionBar without handling Up button event

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have two activities that I want this navigation to happen, they are VendorsActivity and QuestionsActivity. The following how my AndroidManifest.xml looks like:

(I am not using the full name of my activities like com.hello.world.MyActivity as I am defined package attribute in manifest node.)

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".VendorsActivity"
        android:label="@string/vendors_activity_title" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".TestsActivity"
        android:label="@string/tests_activity_title"
        android:parentActivityName=".VendorsActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".VendorsActivity" />
    </activity>
    <activity
        android:name=".QuestionsActivity"
        android:label="@string/questions_activity_title" >
    </activity>
</application>

And in TestsActivity, I am calling getActionBar().setDisplayHomeAsUpEnabled(true); method from within onCreate method.

The problem is, it won’t work unless I implement the following method in .TestsActivity class:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(TestsActivity.this);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

But Android Developer Guide says that I don’t have to handle the Up button’s event as mentioned at the very bottom of hit page: https://developer.android.com/training/basics/actionbar/adding-buttons.html

Because the system now knows MainActivity is the parent activity for
DisplayMessageActivity, when the user presses the Up button, the
system navigates to the parent activity as appropriate—you do not need
to handle the Up button’s event.

Edit and Answer:

As Android Developer Guide says:

Beginning in Android 4.1 (API level 16), you can declare the logical
parent of each activity by specifying the android:parentActivityName
attribute in the element.

If your app supports Android 4.0 and lower, include the Support
Library with your app and add a element inside the
. Then specify the parent activity as the value for
android.support.PARENT_ACTIVITY, matching the
android:parentActivityName attribute.

So I think my problem was because of two reasons:

  1. Running the app on a proper emulator. I was targeting a higher version but the emulator was running on API 14 (Android 4.0) so it didn’t know how to handle android:parentActivityName attribute.

  2. Targeting the right API level in Project Build Target properties as shown below:

enter image description here

How to&Answers:

android:parentActivityName attribute supported only after API level.

One alternative is using support-library:v7 combined with NavUtils.

There is a great training material about this topic (include compatibility issue).
please check
http://developer.android.com/training/implementing-navigation/ancestral.html

That being said, i am posting my code below because it is working :-

MainActivity.Java

package com.example.activitydatasend;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button click = (Button) findViewById(R.id.click);

        click.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainActivity.this,Second.class);
                intent.putExtra("first", "first");
                intent.putExtra("second", "second");
                startActivity(intent);

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

SecondActivity.java

package com.example.activitydatasend;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class Second extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        Bundle bundle = getIntent().getExtras();
        String a = bundle.getString("first");
        String b = bundle.getString("second");
        System.out.println(a+b);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

AndroidManifext.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activitydatasend"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.activitydatasend.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/click"
        android:text="Click" />

</RelativeLayout>

second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Answer:

If you receive an error similar to

E/NavUtils﹕ getParentActivityIntent: bad parentActivityName

or

does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data>  element in your manifest?)

You will probably need to change android:value to the full path.
So instead of

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

do

 <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.package.SecondActivity" />

Answer:

you can add metadata in manifest to support pre JellyBean OS.

<meta-data
     android:name="android.support.PARENT_ACTIVITY"
     android:value=".VendorsActivity" />

Answer:

What API version are you using? In my experience, for older versions it is necessary to handle the up button event yourself, whereas with later versions it’s done for you.

I haven’t looked into which exact version changes this, but I know that API 13 doesn’t handle it for you, while API 16 does.