How to remove specific activity from Android stack?

In my application, I have five actions a, b, c, d, e. The user goes to the following sequence .... 1. a → b 2. b → c 3. c → d 4. d → e

before operation "d", if the user presses the "Back" button, the application should redirect the user to previous activity, for example, d → c, c → b, etc.

But when the user clicks the "Save" button in the "d" action, the application redirects the user to the "e" activity. Now, if the user clicks the back button, I want to redirect the user to operation "a", which is the home screen in my application.

I am completely new to android. I do not know how to achieve this flow. I tried this solution, but it did not give the desired result . and sorry for my bad english ...

0
source share
3 answers

Try this in one.

// Add activity public static void addActivities(String actName, Activity _activity) { if (Config.screenStack == null) Config.screenStack = new HashMap<String, Activity>(); if (_activity != null) Config.screenStack.put(actName, _activity); } // Remove Activity public static void removeActivity(String key) { if (Config.screenStack != null && Config.screenStack.size() > 0) { Activity _activity = Config.screenStack.get(key); if (_activity != null) { _activity.finish(); } } } 

The user adds actions before setting setContentView to the stack.

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addActivities("DemoActivity", DemoActivity.this) setContentView(R.layout.activity_create_feed_post); } 

If you want to finish all the actions when you exist from within the application, you can see this code. p>

+1
source

You can start your homework and clear all previous history. Just add to the intent:

 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 

But it only works if your API level is> 10

0
source
 public class ActivityHandler{ private static HashMap<String, Activity> screenStack; // Add activity public static void addActivities(String actName, Activity _activity) { if (screenStack == null) { screenStack = new HashMap<String, Activity>(); } if (_activity != null && !screenStack.containsKey(actName)) screenStack.put(actName, _activity); } // Remove Activity public static void removeActivity(String key) { if (screenStack != null && screenStack.size() > 0) { Activity _activity = screenStack.get(key); if (_activity != null && !_activity.isDestroyed() ) { _activity.finish(); screenStack.remove(key); } } }} 

In my application ... I used the following lines to add or remove activity from the stack .... To add activity .... ActivityHandler.addActivities("CheckoutActivity",CheckoutActivity.this);

To remove an activity ... ActivityHandler.removeActivity("CheckoutActivity");

0
source

All Articles