Java tutorial
//package com.java2s; //License from project: Open Source License import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import java.util.List; import java.util.Stack; public class Main { public static void replaceFragment(FragmentManager fragmentManager, int frameId, Fragment fragmentToShow, String fragmentTag, Stack<Fragment> fragmentStack) { if (fragmentToShow == null) { return; } List<Fragment> fragmentList = fragmentManager.getFragments(); for (Fragment fragment : fragmentList) { if (fragment == null) { continue; } fragmentManager.beginTransaction().remove(fragment).commit(); if (!fragmentStack.empty()) { fragmentStack.pop(); } } fragmentManager.beginTransaction().add(frameId, fragmentToShow, fragmentTag).commit(); fragmentStack.push(fragmentToShow); } }