set Fragment - Android User Interface

Android examples for User Interface:Fragment

Description

set Fragment

Demo Code


//package com.java2s;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class Main {
    public static void setFragment(FragmentActivity activity,
            Fragment fragment, int containerViewId, boolean addToBackStack) {
        FragmentManager fragmentManager = activity
                .getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();//from  ww  w .  j av  a2 s.co  m
        if (addToBackStack) {
            fragmentTransaction.addToBackStack(fragment.getTag());
        }
        fragmentTransaction.replace(containerViewId, fragment);
        fragmentTransaction.commit();
    }
}

Related Tutorials