replace Fragment - Android android.support.v4.app

Android examples for android.support.v4.app:Fragment

Description

replace Fragment

Demo Code

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
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;
        }// ww  w  .java  2  s .  c o m

        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);
    }

}

Related Tutorials