Java tutorial
//package com.java2s; 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; public class Main { /** * This method loads fragment in a backstack. * * @param fragmentActivity * @param fragmentContainerId * @param fragmentClass * @param bundle * @param tag * @return true if loaded successfully, false otherwise */ public static boolean loadFragmentInBackstack(FragmentActivity fragmentActivity, int fragmentContainerId, Class<? extends Fragment> fragmentClass, Bundle bundle, String tag) { // TODO Auto-generated method stub boolean status = false; try { FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out); Fragment fragment = fragmentClass.newInstance(); fragment.setArguments(bundle); fragmentTransaction.replace(fragmentContainerId, fragment, tag).addToBackStack(null).commit(); // finish pending transactions fragmentManager.executePendingTransactions(); status = true; } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } return status; } }