0

My Android app has a requirement where a certain flow has 7 different screens. Now each of these screens has a common top and bottom. So i have chosen to create a FragmentActivity and 7 different Fragments. How do I insert the fragments into the FragmentActivity at runtime? I have read this tutorial here, and according to this tutorial my main FragmentActivity should have the following layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/fragment_content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

And it should use the following code to replace the fragment:

 FragmentManager fm = getSupportFragmentManager();
 Fragment fragment = fm.findFragmentById(R.id.fragment_content); 

 if (fragment == null) {
     FragmentTransaction ft = fm.beginTransaction();
     ft.add(R.id.fragment_content, new BasicFragment());
     ft.commit();
 }

What I don't understand is the following line:

 ft.add(R.id.fragment_content, new BasicFragment());

R.id.fragment_content is a FrameLayout, will this insert the fragment into the FrameLayout or what?

Rolf ツ
  • 8,611
  • 6
  • 47
  • 72
user1730789
  • 5,157
  • 8
  • 36
  • 57
  • It should be using a `tag` to identify the `Fragment`. You use `id` if your `Fragment`s are in xml, which is not the case here, you are adding them programmatically (the better approach). – Steven Byle Apr 07 '13 at 19:44

2 Answers2

0

R.id.fragment_content is a FrameLayout ? will this insert the fragment into the framelayout or what ?

From what I remember Fragment layout is being placed on top of it. Underneath it, it has another layout that is a "sticker gluing" it to the container layout. So it's a cake in a way. To get a hold of that "sticker", I think, you may call .getParent() on root view of a Fragment.

Oh, and tag fragments to easily find them through FragmentManager(though tag lookup is a tad expensive).

Joel Bodega
  • 644
  • 5
  • 8
0

You can look at this like you have a pool (your FrameLayout in this case) that you can throw toy ships into (your Fragments). So basically you need an environment to contain you Fragments and it could be any layout you choose.

So what you are doing here:

Fragment fragment = fm.findFragmentById(R.id.fragment_content); 

is wrong because R.id.fragment_content is not a Fragment but a FrameLayout.

but it could be your Fragment container, So you need to create a class that extends Fragment with it's own layout and do the operation you did here:

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_content, new BasicFragment(), tag);  //add a tag to a fragment during the transaction so you could easily retrieve it later.
ft.commit();

and of course you could understand a lot more about fragments by reading this page:

http://developer.android.com/guide/components/fragments.html

Emil Adz
  • 40,709
  • 36
  • 140
  • 187