Skip to content Skip to sidebar Skip to footer

Android Backstack Clear But Keep Root

I am currently writing a chat app. It is basically very similar to WhatsApp: On Startup there is last conversation overview. When I want to start a new conversation with somebody

Solution 1:

Define Activity A1's android:launchMode as singleTop at your manifest. Handle back button press at Activity A4 and start your Activity A1 like below:

Intent intent = newIntent(A4.this,  A1.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent); 

With this your A2, A3 will be cleared from stack.

See more for intents.

Edit: This will work for your [A1, A2, A3 , A4] -> back -> [A1] requirement

Solution 2:

Try to do it consistent with the Android design. The back button goes back one activity.

Instead use the home button of the Action Bar.

Android design pattern for navigation;

Create the ActionBar:

finalActionBaractionBar= getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayHomeAsUpEnabled(true);

Go back to top intent when HomeButton got clicked:

final Intent intent = newIntent(this, MainPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Post a Comment for "Android Backstack Clear But Keep Root"