com.sociablue.nanodegree_p1.utils.FragmentRouter.java Source code

Java tutorial

Introduction

Here is the source code for com.sociablue.nanodegree_p1.utils.FragmentRouter.java

Source

/*
 * Copyright (c) 2015. Sociablue Labs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sociablue.nanodegree_p1.utils;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

/**
 * Created by okikejiani on 2015-08-31.
 * Utility to Manage fragment routing.
 */
public class FragmentRouter {

    public static void replace(FragmentActivity activity, Fragment fragment, int container,
            boolean addToBackStack) {

        //get fragment transaction question
        FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
        // Add the fragment to the 'fragment_container' FrameLayout
        transaction.replace(container, fragment);
        //Save Transaction to back stack so hardware back button will undo change.
        if (addToBackStack) {
            transaction.addToBackStack(null);
        }
        //commit fragment change.
        transaction.commit();
    }
}