Method to navigate fragment with customized fragment transition. - Android User Interface

Android examples for User Interface:Fragment

Description

Method to navigate fragment with customized fragment transition.

Demo Code


//package com.java2s;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class Main {
    /**//from  ww  w . jav a  2s  .  c o m
     * Method to navigate fragment with customized fragment transition.
     *
     * @param idContainer Layout id of the container for the fragments in the activity
     * @param activity    current activity for the fragment
     * @param fragment    new fragment to be placed in the activity
     * @param tag         Tag of the new fragment
     * @param bundle      Bundle
     */
    public static void navigateFragment(int idContainer, Activity activity,
            Fragment fragment, String tag, Bundle bundle) {
        FragmentManager fragmentManager = activity.getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
        boolean fragmentPopped = fragmentManager.popBackStackImmediate(tag,
                0);
        /*   *//*
                 * fragment not in back stack, create it.
                 *//* */
        if (!fragmentPopped
                && fragmentManager.findFragmentByTag(tag) == null) {
            if (bundle != null) {
                fragment.setArguments(bundle);
            }
            fragmentTransaction.replace(idContainer, fragment, tag);
            fragmentTransaction.addToBackStack(tag);
            fragmentTransaction.commit();
        }
    }
}

Related Tutorials