Replace the fragment - Android User Interface

Android examples for User Interface:Fragment

Description

Replace the fragment

Demo Code


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

public class Main{
    /**/*w  w w  .ja v a  2 s.co  m*/
     * Replace the fragment
     *
     * @param context
     * @param containerId
     * @param contentFragment
     */
    public static void replace(FragmentActivity context, int containerId,
            Fragment contentFragment) {

        FragmentTransaction fragmentTransaction = context
                .getSupportFragmentManager().beginTransaction();
        fragmentTransaction.setCustomAnimations(
                android.support.design.R.anim.abc_fade_in,
                android.support.design.R.anim.abc_fade_out);
        fragmentTransaction.replace(containerId, contentFragment)
                .commitAllowingStateLoss();

    }
    /**
     * Replace the fragment
     *
     * @param context
     * @param containerId
     * @param contentFragment
     */
    public static void replace(FragmentActivity context, int containerId,
            Fragment contentFragment, int entryAnimation, int exitAnimation) {

        FragmentTransaction fragmentTransaction = context
                .getSupportFragmentManager().beginTransaction();
        //        fragmentTransaction.setCustomAnimations(entryAnimation, exitAnimation,
        //                R.anim.anim_slide_in_right, R.anim.anim_slide_out_right);
        fragmentTransaction.replace(containerId, contentFragment)
                .commitAllowingStateLoss();

    }
}

Related Tutorials