Replace current Fragment with new one. - Android User Interface

Android examples for User Interface:Fragment

Description

Replace current Fragment with new one.

Demo Code


//package com.java2s;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.util.Log;

public class Main {
    /**/*from   w w  w  .j a v a 2  s  . co  m*/
     * Replace current Fragment with new one.
     *
     * @param newFragmentLayoutId
     * @param newFragment
     */
    public static void prepareAndShowFragment(Activity fragmentActivity,
            int newFragmentLayoutId, Fragment newFragment,
            boolean addToBackstack, String tag) {
        // Add the fragment to the activity, pushing this transaction on to the back stack.
        Log.e("FRAGMENT BACKSTACK", "Before count: "
                + fragmentActivity.getFragmentManager()
                        .getBackStackEntryCount() + " TAG: " + tag);
        FragmentTransaction ft = fragmentActivity.getFragmentManager()
                .beginTransaction();
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN
                & FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
        if (addToBackstack) {
            ft.addToBackStack(null);
        }
        ft.replace(newFragmentLayoutId, newFragment, tag);
        ft.commit();
        Log.e("FRAGMENT BACKSTACK", "After count: "
                + fragmentActivity.getFragmentManager()
                        .getBackStackEntryCount() + " TAG: " + tag);
    }
}

Related Tutorials