org.kaaproject.kaa.demo.cityguide.util.FragmentUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.kaaproject.kaa.demo.cityguide.util.FragmentUtils.java

Source

/**
 *  Copyright 2014-2016 CyberVision, Inc.
 *
 *  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 org.kaaproject.kaa.demo.cityguide.util;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import org.kaaproject.kaa.demo.cityguide.R;

/**
 * A simple utility class which provides static functions for managing fragments
 */
public class FragmentUtils {

    private static final String TAG = Utils.class.getSimpleName();

    public static void addBackStackFragment(FragmentActivity activity, Fragment fragment) {
        if (activity == null) {
            Log.e(TAG, "Unable pop fragment. Invalid args.");
            return;
        }
        FragmentManager fm = activity.getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.container, fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.addToBackStack(null);
        ft.commit();
    }

    public static void popBackStack(FragmentActivity activity) {
        if (activity == null) {
            Log.e(TAG, "Unable pop fragment. Invalid args.");
            return;
        }

        FragmentManager fm = activity.getSupportFragmentManager();
        if (fm.getBackStackEntryCount() > 0) {
            fm.popBackStack();
        }
    }

}