Diego Moreno
Diego Moreno

Reputation: 15

Cannot navigate up to Main activity

I have 3 activities (A -main activity-, B and C) on my application. From activity A I can navigate to B, and from B to activity C.

When back button is pressed (or up navigation button is clicked) current activity is finished and parent activity is started (A is B parent, and B is C parent):

    <activity
        android:name=".A"
        android:label="@string/app_name"
        android:noHistory="true"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".B"
        android:label="@string/title_b"
        android:noHistory="true"
        android:parentActivityName=".A"
        android:theme="@style/AppTheme.NoActionBar">

        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.sim.muii.camarerocamarero.A" />
    </activity>
    <activity
        android:name=".C"
        android:label="@string/title_c"
        android:noHistory="true"
        android:parentActivityName=".B">

        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.sim.muii.camarerocamarero.B" />
    </activity>

If I navigate from A down to B, and then return to A (via up navigation), it works correctly. But if I go from A to B, and then from B down to C it allows me to return to B, but when I try to go back from B to A application is closed.

This is the function used to navigate up to parent activity:

@Override
public void onBackPressed() {
    Intent intent = NavUtils.getParentActivityIntent(this);
    NavUtils.navigateUpTo(this,intent);
}

Since my app supports older versions of Android, I've tested it in several devices, and it is correctly working on GingerBread but not in Lollipop. I guess the problem may come from some functionality change in newer versions.

Upvotes: 1

Views: 439

Answers (1)

Onkar Nene
Onkar Nene

Reputation: 1379

Firstly don't use <meta-data> tag in manifest file for navigation purpose. Instead use Explicit intent as you know the target activity

Try this -

From A to B

In activity A - use below code on button click

Intent intentAtoB = new Intent (A.this,B.class);
startActivity(intentAtoB);
finish();

From B to C

1) In activity B - use below code on button click

Intent intentBtoC = new Intent (B.this,C.class);
startActivity(intentBtoC);
finish();

2) In activity B - just override onBackPressed()

@Override
public void onBackPressed() {
    Intent intent = new Intent (B.this,A.class);
    startActivity(intent);
    finish();
}

From C to B

In activity C - just override onBackPressed()

@Override
public void onBackPressed() {
    Intent intent = new Intent (C.this,B.class);
    startActivity(intent);
    finish();
}

Hope this will help :)

Upvotes: 1

Related Questions