Android Fragment management - android

I am learning Android Apps development and in this guide, the API makes it clear a Fragment can be managed in at runtime, so far you have the fragment's id or tag.
But this tutorial in the other hand says you can only deal with fragments at runtime IF you don't declare them in the XML.
I find this very confusing...Which one is right?

If you declare a fragment in a XML layout with the <fragment> tag, you cannot replace it dinamically at runtime.
If you declare in xml layout a container(FrameLayout) for different fragments. Using FragmentTansaction you will be able to add a fragment to that container and then replace it or remove it at runtime. As you will see in the Building a Flexible UI lesson:
In order to replace one fragment with another, the activity's layout
includes an empty FrameLayout that acts as the fragment container.

Related

Which Layout to use with Fragments?

Recently delve into fragments and from what I understand to create a fragment you need a java class and the fragments layout. This makes sense. However what I cant seem to wrap my head around is what "container", or layout do I use to store/insert the fragment? In android studio you can use this to insert fragments, or you can use any other of the layouts. But which one is ideal to use?
Also I saw in a reddit post that I shouldn't be using fragments at all and that its preferred to instead use Frame layouts and play around with your views visibility for the desired effects. Is this true?
You're slightly over-complicating the concept of Fragments.
Fragments, like Activities, don't actually need a dedicated fragments layout xml file. If you choose to do so, you can create the entire layout through Java code, not that I will understand why you'll choose to do so.
So for Fragments, you don't need a Java class and a fragment's layout file. The only requirement is the Java class, and the layouts file is just the preferred approach to inflating a layout, similar to how it is for Activities.
As for your question about the container of the fragments, it's really a matter of your app's design.
You can add a Fragment to your Activity or other Fragments, through the FragmentManager in code or through the <fragment> tag in your layout.xml files.
Neither of those are the best way or the preferred way, since it really depends on what your app needs.
Using the <fragment> tag will cause that fragment to always be added whenever the layout is inflated. This is actually VERY bad if your Activity requires dynamically switching Fragments due to your use of things like ViewPagers, Tabs, Drawer Navigation, or etc. However, it's GREAT if there's no need to dynamically switch fragments and for that specific Activity or parent Fragment, this fragment is a fragment that's always loaded.
For example, let's say you designed a flexible AddNew Fragment that's used in a Dialog and an AddNewActivity. Due to reusing the same screen and code, you decide to make this part of your code a fragment so you can insert it inside a DialogFragment or into another Activity. But, for those DialogFragments and Activities, the only Fragment it'll have is the AddNewFragment, so it'll make sense to just insert that fragment into the Dialog layout and Activity layout through the <fragment> tag.
As for the option with Java code, the preferred approach is to use a FrameLayout. But there's no need to play around with any View visibilities!
The common approach is to just use:
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
A FrameLayout is used because it's going to be the container of the Fragment. In other words, the Fragment will be stored inside of this layout.
So in Java code, you can simply use this code to replace the Fragment inside the container with your new one:
getSupportFragmentManager().beginTransaction().replace(R.id.container, AddNewFragment.newInstance()).commit();
Optionally, you can use add() instead of replace() if you want the fragment to be placed ontop of another fragment within the FrameLayout container.
So yes, to give a decisive answer to your question, there's no ideal way to add a Fragment to an Activity or another Fragment. Each option has it's benefits and drawbacks, with some working better for certain situations and others working better for others.
In the end, it really depends on what your App needs. If you need your Fragments to be flexible, so you can switch Fragments, then this must be done through Java code, because fragments added through the <fragment> tag can't be removed at runtime. However, if you don't need your Fragment to be replaced and it's definitely always going to be showing the same Fragment, then using the <fragment> tag removes the need to write extra Java code to load the dedicated Fragment.
One thing I really do need to point out is... that reddit page you read about is wrong. The 'preferred' way to use Fragments is not to use FrameLayouts and play around with View visibilities. I actually have no idea why there's even a need to change View visibilities.
You can use the new androidx.fragment.app.FragmentContainerView to get a better performance than FrameLayout.
Here more information.
You could use the Fragment layout, but this isn't very flexible. If you're just showing one Fragment, it should work, but using a FrameLayout and inserting your Fragment into it works better as this allows you to change them on the fly.
You may see a FrameLayout with the id R.id.container or something similar, and what this is used for is the Transaction.
For example, if you want to insert FragmentOne into your layout, you can just do this and it'll put it into R.id.container.
Fragment fragment = new FragmentOne();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.commit();

Why should I not add Fragments to the activity layout XML file?

Edit: source : http://developer.android.com/training/basics/fragments/creating.html last parargraph says :
Note: When you add a fragment to an activity layout by defining the
fragment in the layout XML file, you cannot remove the fragment at
runtime. If you plan to swap your fragments in and out during user
interaction, you must add the fragment to the activity when the
activity first starts, as shown in the next lesson.
Thanks in advance.
You can use fragments in two ways ,
Static Fragments
Here you can define the fragment in whatever the layout file you need. Only thing is, that defined fragment can not be change at the run time. So, re-usability will be issue here, you cant take the advantage of the re-usability of fragments in this case.
Dynamic Fragments
Here you can define a place holder(frame layout etc) on your layout and you can add/replace whatever the fragment you expect at any time while your activity is running. This ensure the re-usability.
Also you can use backStack if back navigation is required.
So, it depend on your requirement.
If the fragments are defined in Xml you won't be able to change them at runtime; in that case I'd suggest using an <include layout="#layout/my_inner_layout"/> tag, that is not only better performance-wise as it is easier to use (you can access the views defined in my_inner_layout.xml using findViewById() method from the host activity).

Difference between Layout and Fragment Layout

When creating an new Android Activity, it used to create an xml file in the layouts folder where I would define the UI.Now it creats two files:
1.Layout file
2.Fragment Layout File.
Could someone explain the difference between the two? Also when trying to add items as listviews, buttons...etc. in which file should I add them to be called in my activity file.
Starting with Android 3.0, Activities may now host Fragments which can be used to develop portions of the UI, and be displayed in different configurations depending on screen size, orientation, and other factors. It is highly recommended to use Fragments in modern Android applications, but is not required.
You can create an Activity layout which will hold one or more Fragments, and then place your UI components in the Fragment's layout. The Activity will load the Fragment, and then the Fragment will inflate the layout you wish to present inside it. You can also dynamically add/remove/swap out different Fragments inside the same Activity, depending on what you wish to display to the user.
You can read more about how to use Fragments here: Fragments | Android Developers
You can also choose to ignore the Fragment design principle, and continue to place all of your layouts into the Activity layout file. In that case, you can delete the Fragment layout.
The default structure of new Android projects has changed since a recent update of the adt:
How it is now: There will be a Fragment "PlaceHolderFragment" created which uses the fragments layout. The other layout is the one that the Activity uses.
How it was before: No Fragment was generated after creating a new project, so there was also no fragment layout needed.
==> You have to decide if you actually want to use Fragments now. If so use the fragment layout and learn how to use Fragments in Android. If you decide that you don't need to use Fragments for now, then you can just delete the PlaceHolderFragment code & delete the fragments layout.
In short, before, it was an activity per its layout file, but now one activity could have more layouts, but the second layout file is automatically generated as a fragment to avoid collision while the android studio is matching the resources, similarly two or more activities could share the same fragment too.

Why Android put Fragments inside FrameLayouts?

Why Google usually put Fragments inside a FrameLayout. I see it in the examples and in the source code of the Google I/O App. I tried to translate the properties of the FrameLayout to the properties of the Fragment and all stayed equal as expected, so why do they use a FrameLayout container?
Probably because that way you can easily make it into a tabbed navigation, since TabHost extends FrameLayout, you can assign the FrameLayout resource to it.

Multiple fragment instances from XML as pages in a ViewPager - how do add a sub-fragment?

I have multiple instances of a fragment that are inflated from an xml file, with an id assigned there that I had hoped to use as a container root. If I want to attach a sub-fragment to one of these fragments as my container, how do I know which one is used, or better yet how do I select a particular container fragment? Specifically, I want to be able to attach a fragment to one of a set of pages in a ViewPager, each of which is an instance of a common fragment definition. Any one of these pages could serve as the root for the sub-fragment. However, they'll all end up with the same id since it's specified in the XML, and you can't call FragmentTransaction#add using a fragment tag name...
If I want to attach a sub-fragment to one of these fragments as my container, how do I know which one is used, or better yet how do I select a particular container fragment?
Android does not support nested fragments, so you simply do not do it at all.
Now you can use nested fragments - also included in support library
here is my solution
ViewPager and fragments — design nested fragments or ..?

Categories

Resources