Home » Android » android – How can I pop fragment from backstack without resuming it?

android – How can I pop fragment from backstack without resuming it?

Posted by: admin June 15, 2020 Leave a comment

Questions:

How can I pop fragment from backstack without resuming it? I just want to remove from back stack, I don’t want to display it.

How to&Answers:

It’s architectural question.

1)Suppose we have 2 controllers (fragments/activities): controller A and controller B.
Both of them connected to one instance of some Model (you may use binding with service, Singleton pattern or init model in Application-heir class and make a getter to it).
When something interesting happens in controller B, he notifies model about it and then model calls controllerA.finish() / controllerA.remove().
Of course, I always try to implement this solution as nicely as possible, but there is a main idea.

2)In another circumstances, I call finish() immediately after startActivity(intent);

3)Also I may write: startActivityForResult(intent, requestCode) and after finishing second activity method onActivityResult(requestCode, responseCode, intent) is called – if requestCode’s are equal, I finish the activity.

Answer:

If you don’t use a FragmentTransaction’s method
public abstract FragmentTransaction addToBackStack (String name)
to add it to the back stack. There will not be a need to remove it.