replace Fragment From Activity - Android Activity

Android examples for Activity:Activity Fragment

Description

replace Fragment From Activity

Demo Code


//package com.java2s;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class Main {
    public static void replaceFragmentFromActivity(
            @NonNull FragmentManager fragmentManager,
            @NonNull Fragment oldFragment, @NonNull Fragment newFragment,
            int frameId, boolean saveToBackStack, String tag) {
        FragmentTransaction transaction = fragmentManager
                .beginTransaction();//from  w  w w.  j  ava 2  s .co  m
        transaction.remove(oldFragment);
        transaction.add(frameId, newFragment, tag);
        if (saveToBackStack) {
            transaction.addToBackStack(null);
        }
        transaction.commit();
    }
}

Related Tutorials