Is it possible to delete the fragment defined in the layout.xml file?

Is it possible to use FragmentTransaction and the remove () method to get rid of the fragments that are defined in layout.xml (using the fragment tag)?

I was not able to work this using the v4 support libraries. The fragment remains in place after you perform the FragmentTransaction, after calling remove (). Can someone tell me if this is by design, bug or feature?

Can I replace the fragment that is defined in the lyaout.xml file, so I'm a little strange that it could not be deleted?

+10
android android-3.0-honeycomb android-fragments
Jan 18 '12 at 1:00
source share
3 answers

The native APIs running in Honeycomb work the same as in the libarary support, so you cannot delete an instance of the fragment that was declared in your layout XML file.

With FragmentTransactions, you manipulate ViewGroups such as LinearLayouts, which act as containers for storing the layout of other fragments. However, when you declare a Fragment in your layout, it does not have a container in the same sense, because it is constantly part of the View hierarchy, so you cannot delete it. This is by design to support things like navigational snippets that you never delete anyway. :)

One thing, interesting, and I discovered this by accident, is that you can add new fragments to a fragment that was declared with a tag in your layout; and it acts like a container for other fragments

+11
Jan 18 2018-12-18T00:
source share

I did not work using v4 support libraries. The fragment remains in place after you make the FragmentTransaction, after calling Delete (). Can someone tell me if this is by design, bug or feature?

This is by design (or lack of function, not definitely a function, if you ask me: P). Therefore, while you are using support libraries, you cannot achieve this.

+1
Jan 18 2018-12-18T00:
source share

Like @ david-c-sainte-claire and @ martΓ­n-marconcini, you cannot use the remove () method and FragmentTransaction to remove a fragment that has been defined in XML. This does not mean that you are out of luck. You can always use the setVisibility () method.

 findViewById(R.id.fragment_main).setVisibility(View.GONE); 
+1
Oct 06 '16 at 12:42 on
source share



All Articles