Correct way to close fragments

There are two parts to this question.

Let's say we have an Activity and then two Fragments: ListFragment and Fragment (which will be displayed when clicking an item from the list).

Part 1

Where should I close the fragment? By this I mean what would be considered good from a design point of view. I see two options, one declaring an interface in a fragment and having an implementation of it, call it closeFragment (). This will be a way to communicate from a snippet with an activity as shown on the Dev site. The other is probably pretty simple and calls getActivity (). GetSupportFragmentManager () and uses the dispatcher to close it.

Part 2

I know how to create a snippet and replace it since it was created on the Dev site, but I have doubts about closing it. How can I close it? Is something like the following code correct? Let's assume a snippet has been added to the BackStack.

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
getSupportFragmentManager().popBackStack();
transaction.remove(this);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
transaction.commit();

      

Many thanks.

+3


source to share


2 answers


  • I would rather have a dumb snippet that knows nothing about where it is being used, so that you can use it for whatever activity you want, and it has the exact target you set for it. Of course, you can do whatever you want.

  • It looks like a closure, but I'd rather replace it instead. You can also always go back to a snippet if you have a link to it.



+2


source


FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.remove(this);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
ft.commit();

      



+2


source







All Articles