1

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...

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Intimate
  • 446
  • 2
  • 5
  • 18

2 Answers2

3

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) {
            Config.screenStack.remove(key);
            _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);
}

If you want to finish all activity when you exist from app you can see this code.

Andy Developer
  • 3,071
  • 1
  • 19
  • 39
  • Thanks for solution. your solution is the exact thing that i want. It will give me control over back stack as i want. but where should i implement addActivities() and removeActivity() methods, so that i can use them from any activity? plz, help me.... – Intimate Jul 13 '16 at 05:43
  • You can put this both files in Utils class so that in your activity you can use something like this. Utils.addActivities("DemoActivity", DemoActivity.this); Thanks...! – Andy Developer Jul 13 '16 at 05:47
  • Just change the Utils.addActivities("activity name", activity.this); according to your activity. Thanks! – Andy Developer Jul 13 '16 at 05:59
  • 1
    thanks bro!!!! it worked for me....but i edited code a little bit...i posted it in answer..... – Intimate Jul 13 '16 at 07:50
2
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 following lines to add or remove activity from stack.... To add activity.... ActivityHandler.addActivities("CheckoutActivity",CheckoutActivity.this);

To remove Activity... ActivityHandler.removeActivity("CheckoutActivity");

Intimate
  • 446
  • 2
  • 5
  • 18