Java tutorial
//package com.java2s; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; public class Main { public static void replaceFragment(FragmentManager manager, Class<? extends Fragment> fragmentClass, boolean isAddToBackStack) { Fragment fragment = manager.findFragmentByTag(fragmentClass.getSimpleName()); if (null == fragment) { try { fragment = fragmentClass.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } FragmentTransaction ft = manager.beginTransaction(); ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out); if (!fragment.isAdded()) { ft.replace(android.R.id.content, fragment, fragment.getClass().getSimpleName()); if (isAddToBackStack) { ft.addToBackStack(null); } } ft.commit(); } }