Android Back navigation

4.4k Views Asked by At

I have the following activities: A -> B -> C -> D. If I press the back button from Activity D I want to go back to A. This is the code:

Activity A

// Launch Activity B
Intent intent = new Intent();
intent.setClass(ActivityA.this, ActivityB.class);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Activity B

 // Launch Activity C
 Intent intent = new Intent();
 intent.setClass(ActivityB.this, ActivityC.class);

 startActivity(intent);

Activity C

// Launch Activity D
Intent intent = new Intent();
intent.setClass(ActivityC.this, ActivityD.class);

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);
finish();

In Activity D I press back and I want to go back to Activity A. I do not want to use finish() each time I open an activity because when I am in Activity C I want to be able to go back to B, but when I am in Activity D, I want to go directly to A, but unfortunately the current implementation makes the app to close.

4

There are 4 best solutions below

0
On

Maybe this will work out:(Remember to override the onBackPressed)

Intent intent = new Intent(this,A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Meaning of the Flag:

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity A, then B, C and D will be finished and A receive the given Intent, resulting in the stack now being: A.

0
On

You could capture the back button on Activity D and then start Activity A with the flag FLAG_ACTIVITY_CLEAR_TOP.

// For Android 2.0 and above
@Override
public void onBackPressed() {

    Intent intent = new Intent(currentActivity.this, ActivityA.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   
    startActivity(intent);
}

See here for explanation of FLAG_ACTIVITY_CLEAR_TOP

You can also Provide Up Navigation as another example suggests. However this works differently to the back press see here for comparison

1
On

if you want to simplfy you can try Up Navigation for this enter image description here

<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>
0
On

override back button method and add custom navigation:

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