EGHDK
EGHDK

Reputation: 18130

How to use Fragments in Android properly

I asked a question a while back:

Replacing Fragments isn't working/Am I executing this the proper way?

Because I was having a really tough time wrapping my head around the entire concept. Past Google I/O videos have also helped, but I still didn't really have a reason to use fragments. I have encountered my problem again, and with more searching and Google I/O 2013 videos, I've hit another fork in the road. After starting a bounty to get a more expert opinion on this subject, I was guided to use FrameLayouts, yet in this I/O 13 video at 19:05 into the video it shows <fragment>. Can someone clear up any discrepencies or misconceptions I have?

Do I use <fragment> or <frameLayout>? Bonus points if you look at my original question to know where I'm coming from.

Upvotes: 1

Views: 635

Answers (1)

Rarw
Rarw

Reputation: 7663

There are 2 seperate questions I see in here; (1) what is the <fragment> element? (2) am I (meaning you) doing what I tried to do in my previous question in the best way possible by using a FrameLayout?.

I will try to answer both.

The <fragment> XML tag is just another way to implement a fragment. As a refresher, a Fragment is like a part of an activity. It is a stand alone class, however, referring back to the fragment documentation, because a Fragment is intended to be modular, all fragments must be housed within a seperate FragmentActivity. Think of this as FragmentActivity as a container. By loading your Fragment into the FragmentActivity the FragmentActivity provides other valuable functions that the Fragment class does not have access to on its own. It also offers you the chance to swap out fragments dynamicly, allowing for a more flexable user experience.

How you load your Fragment into its containing FragmentActivity depends on what you are trying to acomplish. One way to do this is to use the <fragment> tag. With this approach, rather than declaring the Fragment in the activity java code and then loading it with a FragmentTransaction as you did in your earlier question, the Fragment class is loaded via the XML layout for the containing FragmentActivity.

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"         
    android:orientation="horizontal"     
    android:layout_width="fill_parent"     
    android:layout_height="fill_parent">    


    <fragment 
        android:name="com.example.android.fragments.HeadlinesFragment"                 
        android:id="@+id/headlines_fragment"               
        android:layout_weight="1"               
        android:layout_width="0dp"               
        android:layout_height="match_parent" />      

</LinearLayout> 

You can see the fragment declared here in the example taken from the android docs. The fragment is then loaded by the FragmentActivty when setContentView() is called onCreate.

public class MainActivity extends FragmentActivity {     
    @Override     
    public void onCreate(Bundle savedInstanceState) {         
    super.onCreate(savedInstanceState);         
    setContentView(R.layout.news_articles);     
    } 
}

The other way to display fragments is as you have done it in your prior example. Using a FragmentManager you can create a new instance of your Fragment class and load it programatically within the java code.

So which technique makes sense when? As the example from the android docs demonstraits, the XML fragment method makes sense when you are using stationary fragments, i.e. you are not dynamically swapping fragments in and out. This is because you cannot edit the XML layout you create on the fly the same way you can the java code. Also, from what I can tell, using the XML approach treats the fragments more like layout elements alowing for different control. In the android example, they use the XML approach to display 2 fragmens side by side on a tablet. By controling the fragments more like layout elements, e.g. having the ability to adjust layout_weight you can achieve a better result for that purpose.

However, if you are designing a highly dynamic UX, say with a ViewPager or some other feature where fragments will be rotated regularly, it makes more sense to use seperate Fragment classes and replace those Fragments as they become necessary.

Based on your prior question and the need to swap fragments dynamicly I think you made the right choice for that implementation.

Upvotes: 4

Related Questions