user3066571
user3066571

Reputation: 1471

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.

Upvotes: 3

Views: 1970

Answers (5)

Daksh Gargas
Daksh Gargas

Reputation: 3913

jitesh mohite's answer has a drawback, if you'll start a new Activity, you'll end up losing all the data on the screen(if any). Launching a Fragment usually causes Activity to pause, and if re-launching an activity was the case, well why not use Activities is the first place?

A FragmentManager always consist of a list of Fragments in the BackStack

You can loop through all the Fragments and pop them out. To clear the Fragments BackStack of a FragmentManager, you could iterate through all back stack items and call popBackStack()

FragmentManager fm = getActivity().getSupportFragmentManager();
for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {    
    fm.popBackStack();
}

But there is also more elegant solution:

// in my case I get the support fragment manager, it should work with the native one too
FragmentManager fragmentManager = getSupportFragmentManager();
// this will clear the back stack and displays no animation on the screen
fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

Upvotes: 0

Aaditya Brahmbhatt
Aaditya Brahmbhatt

Reputation: 417

There are so many ways to do so.

You can clear your FragmentManager's back-stack completely.

OR just restart that activity with

if (getActivity() != null) {
    Intent intent = getActivity().getIntent();
    getActivity().finish();
    startActivity(intent);
}

Upvotes: 0

Chiang Xingbing
Chiang Xingbing

Reputation: 61

I don't think the problem is how to restart the Activity to circumvent data loading. The question should be why your program can't allow returning MainActivity by rolling back the stack and reload data?

Sending a new Intent can really solve your problem, but I think this may not be the most elegant approach.

Generally speaking, elegantly, your data can be shared. After deleting the data, other locations are automatically synchronized; or you are allowed to reload the data and work normally.

Upvotes: 0

Jitesh Mohite
Jitesh Mohite

Reputation: 34180

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.

Upvotes: 4

shizhen
shizhen

Reputation: 12583

You can consider calling finish() or getActivity().finish() method to close the parent activity before you start the downstream activities.

Upvotes: 0

Related Questions