Example usage for android.app FragmentTransaction remove

List of usage examples for android.app FragmentTransaction remove

Introduction

In this page you can find the example usage for android.app FragmentTransaction remove.

Prototype

public abstract FragmentTransaction remove(Fragment fragment);

Source Link

Document

Remove an existing fragment.

Usage

From source file:Main.java

public static void removeFragment(Activity activity, Fragment fragment) {
    if (activity != null) {
        try {/*ww  w .  j  ava 2s . co  m*/
            FragmentManager fragmentManager = activity.getFragmentManager();
            fragmentManager.executePendingTransactions();
            if (fragment.isAdded()) {
                FragmentTransaction ft = fragmentManager.beginTransaction();
                ft.remove(fragment);
                ft.commitAllowingStateLoss();
                fragmentManager.executePendingTransactions();
            }
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

public static void showDialogFragment(final DialogFragment dialog, final String tag,
        final FragmentManager fragmentManager) {
    final FragmentTransaction ft = fragmentManager.beginTransaction();
    final Fragment prev = fragmentManager.findFragmentByTag(tag);
    if (prev != null) {
        ft.remove(prev);
    }/*from  ww w  .  j  av a 2s .  c  om*/
    ft.add(dialog, tag);
    ft.commitAllowingStateLoss();
}

From source file:Main.java

public static void removeDialogFragment(final String tag, final FragmentManager fragmentManager) {
    if (fragmentManager == null) {
        return;/*www  .  j av a 2  s .  c  om*/
    }
    final FragmentTransaction ft = fragmentManager.beginTransaction();
    final Fragment prev = fragmentManager.findFragmentByTag(tag);
    if (prev != null && prev.isAdded()) {
        ft.remove(prev);
    }
    ft.commitAllowingStateLoss();
}

From source file:Main.java

public static void replaceFragmentToLayout(final int containerId, final FragmentManager fragmentManager,
        final Fragment fragment, final String tag) {
    final FragmentTransaction ft = fragmentManager.beginTransaction();
    final Fragment previousFragment = fragmentManager.findFragmentByTag(tag);
    if (previousFragment != null) {
        ft.remove(previousFragment);
    }/*from  w  w  w.  j ava  2 s  . c  om*/
    ft.add(containerId, fragment, tag);
    ft.commit();
}

From source file:com.androidexperiments.sprayscape.androidutils.PermissionRequester.java

public static void requestPermission(final String permission, final String objectName,
        final String methodName) {
    Log.i(TAG, "requestPermission() called for permission: " + permission);

    final Activity currentActivity = UnityPlayer.currentActivity;
    boolean granted = ActivityCompat.checkSelfPermission(currentActivity,
            permission) == PackageManager.PERMISSION_GRANTED;

    if (granted) {
        Log.i(TAG, "Permission already granted: " + permission);
        SendPermissionResult(permission, true, objectName, methodName);
        return;/*from   www  .j  a va 2  s .com*/
    }

    try {
        // use a fragment here because we wouldn't be able to capture onRequestPermissionsResult() on the main activity unless we sub-classed it...
        // final values so the fragment inner-class can access them
        final FragmentManager fragmentManager = currentActivity.getFragmentManager();
        final Fragment request = new Fragment() {

            @Override
            public void onStart() {
                super.onStart();
                Log.i(TAG, "Permission fragment onStart()");
                FragmentCompat.requestPermissions(this, new String[] { permission }, 0);
            }

            @SuppressLint("Override")
            @SuppressWarnings("unused")
            public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
                Log.i(TAG, "onRequestPermissionsResult(" + requestCode + ", " + permissions.toString() + ", "
                        + grantResults.toString() + ")");

                if (grantResults.length > 0) {
                    for (int i = 0; i < grantResults.length; i++) {
                        if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                            Log.i(TAG, "Permission granted: " + permissions[i]);
                            SendPermissionResult(permissions[i], true, objectName, methodName);
                        } else {
                            Log.i(TAG, "Permission denied: " + permissions[i]);
                            SendPermissionResult(permissions[i], false, objectName, methodName);
                        }
                    }
                } else {
                    Log.i(TAG, "Permission denied: " + permission);
                    SendPermissionResult(permission, false, objectName, methodName);
                }

                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.remove(this);
                fragmentTransaction.commit();
            }
        };

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(0, request);
        fragmentTransaction.commit();
    } catch (Exception error) {
        Log.e(TAG, "Permission request failed: '" + permission + "'", error);
        SendPermissionResult(permission, false, objectName, methodName);
    }
}

From source file:com.afollestad.overhear.ui.OverviewScreen.java

@SuppressLint("CommitTransaction")
private static void showAboutDialog(Activity activity) {
    FragmentManager fm = activity.getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag("dialog_about");
    if (prev != null)
        ft.remove(prev);
    ft.addToBackStack(null);/*  w  ww .  j  a  va 2  s. c o m*/
    new AboutDialog().show(ft, "dialog_about");
}

From source file:com.achep.activedisplay.DialogHelper.java

private static void showDialog(Activity activity, Class clazz, String tag) {
    FragmentManager fm = activity.getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag(tag);
    if (prev != null) {
        ft.remove(prev);
    }/*from   w  w w.j  a va2s . c o m*/
    ft.addToBackStack(null);

    try {
        ((DialogFragment) clazz.newInstance()).show(ft, tag);
    } catch (InstantiationException | IllegalAccessException e) {
        e.printStackTrace();
    }
}

From source file:com.readystatesoftware.ghostlog.GhostLogSettingsFragment.java

private static void setupOpenSourceInfoPreference(final Activity activity, Preference preference) {
    preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override/*from   ww  w .j  a v  a  2  s . com*/
        public boolean onPreferenceClick(Preference preference) {
            FragmentManager fm = activity.getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment prev = fm.findFragmentByTag("dialog");
            if (prev != null) {
                ft.remove(prev);
            }
            ft.addToBackStack(null);
            new OpenSourceLicensesDialog().show(ft, "dialog");
            return true;
        }
    });
}

From source file:com.readystatesoftware.ghostlog.GhostLogSettingsFragment.java

private static void setupVersionPref(final Activity activity, Preference preference) {
    preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override/*w w  w.  jav  a2 s.c o  m*/
        public boolean onPreferenceClick(Preference preference) {
            FragmentManager fm = activity.getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment prev = fm.findFragmentByTag("dialog");
            if (prev != null) {
                ft.remove(prev);
            }
            ft.addToBackStack(null);
            new AboutDialog().show(ft, "dialog");
            return true;
        }
    });
}

From source file:com.aoppp.gatewaymaster.dialogs.DialogUtil.java

/**
 * ??Fragment.//w ww .  java2  s.c om
 * 
 * @param context
 *            the context
 */
public static void removeDialog(Context context) {
    try {
        FragmentActivity activity = (FragmentActivity) context;
        FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
        // 
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
        Fragment prev = activity.getFragmentManager().findFragmentByTag(mDialogTag);
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);
        ft.commit();
    } catch (Exception e) {
        // ?Activity??
        e.printStackTrace();
    }
}