Android UI How to - Use FragmentManager








The following code shows how to Use FragmentManager.

Example

Layout xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Go Home"
        android:onClick="onHomeClick" />
    <FrameLayout
        android:id="@+id/container_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

Main activity Java code

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/*  w  ww  .j  a v a2 s .  c  om*/
public class MyActivity extends FragmentActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();        
        ft.add(R.id.container_fragment, MyFragment.newInstance("First Fragment"));
        ft.commit();
        
        ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.container_fragment, MyFragment.newInstance("Second Fragment"));
        ft.addToBackStack("second");
        ft.commit();
        
        ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.container_fragment, MyFragment.newInstance("Third Fragment"));
        ft.addToBackStack("second");
        ft.commit();
        
        ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.container_fragment, MyFragment.newInstance("Fourth Fragment"));
        ft.addToBackStack("fourth");
        ft.commit();
    }
    
    public void onHomeClick(View v) {
        getSupportFragmentManager().popBackStack("second", FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }
    
    public static class MyFragment extends Fragment {
        private CharSequence mTitle;
        
        public static MyFragment newInstance(String title) {
            MyFragment fragment = new MyFragment();
            fragment.setTitle(title);
            
            return fragment;
        }
        
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            TextView text = new TextView(getActivity());
            text.setText(mTitle);
            text.setBackgroundColor(Color.WHITE);
            
            return text;
        }
        
        public void setTitle(CharSequence title) {
            mTitle = title;
        }
    }
}