Intimate Intimate - 4 months ago 19
Android Question

How to remove a particular activity from android back stack?

In my application, i have five activity a,b,c,d,e. User transits in following sequence....
1. a -> b
2. b -> c
3. c -> d
4. d -> e

up to the activity 'd', if user presses the back button, application should redirect user to the previous activity like d -> c , c -> b and so on...

But when user click's on save button in activity 'd', application will redirect user to the activity 'e'.now if user presses the back button then i want to redirect user to the activity 'a',which is home screen in my application.

I am completely new to the android. I don't know how to achieve this flow.I tried this solution but it hasn't yielded desired result. and sorry for my bad English...

Answer

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();
        }
    }
}

User add activities before setContentView to add into the stack.

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