Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android remove Activity from back stack

Tags:

stack

android

Okay so I'm kind of stumped on what to do with this. So I have the MainActivity, and from there an Activity can be launched to DegreePlanActivity, and from there another Activity can be launched to EditDegreePlan. I've got EditDegreePlan set to noHistory in the AndroidManifest. The problem is after they save the EditDegreePlan it launches an Activity to DegreePlan. So if the user presses Back they have to press it twice to get to MainActivity again. I want to get rid of that so they only have to press it once. I'm stumped on how to do this though.

If I set DegreePlanActivity to noHistory then they couldn't press Back to it while in EditDegreePlan.

I've tried overriding onBackPressed method and launching an intent to MainActivity. The problem then is that they have to press Back multiple times to exit the app then.

What should I do?

like image 878
Emrys90 Avatar asked Jan 01 '13 16:01

Emrys90


People also ask

How do you delete back activity on Android?

Use finishAffinity(); in task C when starting task A to clear backstack activities.

How do I delete activity from stack?

The easiest way is to give the LoginActivity a “android:noHistory = true” attribute in the manifest file. That instructs Android to remove the given activity from the history stack thereby avoiding the aforementioned behavior altogether.

How do you exclude activity from the Backstack?

simple solution for API >= 15 to API 23 user activity name in intent. Show activity on this post. To remove activity from back stack inside manifest add android:noHistory="true" to your activity inside the manifest file.

What is activity back stack?

These activities are arranged in a stack—the back stack—in the order in which each activity is opened. For example, an email app might have one activity to show a list of new messages. When the user selects a message, a new activity opens to view that message. This new activity is added to the back stack.


2 Answers

FLAG_ACTIVITY_CLEAR_TOP clears your Activity stack , you can use the code below:

Intent intent = new Intent(this, Activity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); 

Remember that this flag clears just Intermediate Activities , for example if you have A,B,C in your Back Stack then going from C Activity to D with this flag this does not clear Back Stack and the Stack would be A,B,C,D but if you go from Activity D to Activity A with this flag , B,C,D Activities will pop up from the stack and you will have just A in the Back Stack .

like image 187
Arash GM Avatar answered Sep 19 '22 10:09

Arash GM


simple solution for API >= 15 to API 23 user activity name in intent.

 Intent nextScreen = new Intent(currentActivity.this, MainActivity.class);  nextScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);  startActivity(nextScreen);  ActivityCompat.finishAffinity(currentActivity.this); 
like image 21
Kamal Bunkar Avatar answered Sep 21 '22 10:09

Kamal Bunkar