Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how to return to Main Activity without navigating backstack?

I'm operating in a fragment, and one of the options is to delete the thing I'm looking at. Upon confirming the delete action, I want it to just back out to the home screen, or MainActivity, and I don't want to go through the backstack, since that will attempt to traverse over the thing I just deleted.

like image 621
user3066571 Avatar asked Apr 09 '26 19:04

user3066571


1 Answers

You can use below code to clear all previous stack, Launch the same main activity.

Intent i = new Intent(OldActivity.this, MainActivity.class);
// set the new task and clear flags
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |  Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(i);

You can also use ActivityCompat.finishAffinity() to clear all child activities.

like image 70
Jitesh Mohite Avatar answered Apr 13 '26 02:04

Jitesh Mohite



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!