3) Adding fragments using Java code

The Fragment Manager

2014-04-24_19-03-34

Every activity has its own Fragment Manager which is accessible through the method:

getFragmentManager()

The Fragment Manager maintains references to all the fragments within an activity.

You can get references to individual fragments within the activity using the methods

findFragmentById()
findFragmentByTag()

The Fragment Transactions

2014-04-24_19-03-32

Adding, removing, swopping fragments are carried out by Fragment Transactions.

So using the Fragment Manager…

How do we create a fragment?

  • Create a class that extends fragments (so we have fragment methods available to us)
  • Create the appearance in XML or java
  • Use the onCreateView() method to link the appearance to the object
  • … this is where things change now

  • Get a reference to the Fragment Manager
  • MyFragment frag = new MyFragment()
    FragmentManager manager = getFragmentManager()

  • Begin a transaction by calling
  • FragmentTransaction transaction = manager.beginTransaction()

    … now we can call methods on the transaction object to do stuff to our fragment … e.g.

    // takes 3 parameters
    // the id of the layout in which you want to add your fragment
    // the object of the fragment itself which you initialised before
    // a string that can be used to identify the fragment later when you call findFragmentByTag()
    transaction.add(R.id.my_layout, frag, “JamesFragment”);
    transaction.commit()

    Demonstration in code

    MainActivity.java

    package com.jamesfroggatt.fragments3.app;
    
    import android.app.Activity;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // here we're going to add the fragment to out activity in java code
            MyFragment frag=new MyFragment();
    
            FragmentManager manager=getFragmentManager();
    
            FragmentTransaction transaction= manager.beginTransaction();
    
            transaction.add(R.id.my_layout_id_in_activity_main_xml, frag, "James Fragment");
    
            transaction.commit();
        }
    }
    

    activity_main.xml

    Snap 2014-04-24 at 21.57.00

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#44FF33"
    
        android:id="@+id/my_layout_id_in_activity_main_xml" >
        <!-- this id is all the xml we need to get fragment in here-->
        <!--the rest is done in Java -->
    </RelativeLayout>
    

    MyFragment.java

    package com.jamesfroggatt.fragments3.app;
    
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    
    public class MyFragment extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            // link the xml fragment (my_fragment_layout.xml) to the java code for the fragment
            // we need to return a view object
            return inflater.inflate(R.layout.my_fragment_layout, container, false);
        }
    }
    

    my_fragment_layout.xml

    Snap 2014-04-24 at 21.58.35

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#225588" >>
    
        <TextView
            android:layout_margin="60dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="This is my_fragment_layout.xml fragment"
            android:id="@+id/textView" />
    </LinearLayout>
    

    Gives the following result:

    Portrait
    Snap 2014-04-24 at 21.59.31

    Landscape
    Snap 2014-04-24 at 22.00.09

    Leave a Reply