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...
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);
}
If you want to finish all activity when you exist from app you can see this code.
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");
来源:https://stackoverflow.com/questions/38326642/how-to-remove-a-particular-activity-from-android-back-stack