Skip to content Skip to sidebar Skip to footer

Go Back To Mainactivity From Any Activities With Flag

I want to go back to MainActivity from any activities. For ex, my stack of activities: A - B - C - D. A (MainActivity) is in the bottom and D is in the top of stack . When I use a

Solution 1:

I hope this solution solve your problem::- Only set this to Intent

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);

Solution 2:

Find the solution :

Note : It will clear all previous activity and will lauch HomeActivity

Intent homeActivity = newIntent(context, DJ_HomeActivity.class);
homeActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(homeActivity); 

Solution 3:

Try this in one.You can add and remove activity from stack like this.

// Add activitypublicstaticvoidaddActivities(String actName, Activity _activity) {
    if (Config.screenStack == null)
        Config.screenStack = newHashMap<String, Activity>();
    if (_activity != null)
        Config.screenStack.put(actName, _activity);
}

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

For remove activity you just call the removeActivity() and pass the key which you use when add the activity.

Solution 4:

Override onBackpress method of each activity :

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

Solution 5:

you should use below code to jump on the MainActivity.With the intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) following flag of intent it clears the all previous activity.

  Intent intent = newIntent(this, DashboardActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

Note one thing, You used android:launchMode="singleTask"So while moving from A activity to another activity you must have to finish A Activity by using this.finish() method.

Post a Comment for "Go Back To Mainactivity From Any Activities With Flag"