Java Code Examples for androidx.fragment.app.FragmentTransaction#remove()

The following examples show how to use androidx.fragment.app.FragmentTransaction#remove() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LicensesFragment.java    From mimi-reader with Apache License 2.0 6 votes vote down vote up
/**
 * Builds and displays a licenses fragment for you. Requires "/res/raw/licenses.html" and
 * "/res/layout/licenses_fragment.xml" to be present.
 *
 * @param fm A fragment manager instance used to display this LicensesFragment.
 */
public static void displayLicensesFragment(FragmentManager fm, @RawRes int htmlResToShow, String title) {
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag(FRAGMENT_TAG);
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    final DialogFragment newFragment;
    if (TextUtils.isEmpty(title)) {
        newFragment = LicensesFragment.newInstance(htmlResToShow);
    } else {
        newFragment = LicensesFragment.newInstance(htmlResToShow, title);
    }
    newFragment.show(ft, FRAGMENT_TAG);
}
 
Example 2
Source File: SimpleDialog.java    From SimpleDialogFragments with Apache License 2.0 6 votes vote down vote up
/**
 * Shows the dialog. Results will be forwarded to the fragment supplied.
 * The tag can be used to identify the dialog in {@link OnDialogResultListener#onResult}
 * An optional argument can be used to remove a previously shown dialog with the tag given
 * prior to showing this one.
 *
 * @param fragment the hosting fragment
 * @param tag the dialogs tag
 * @param replaceTag removes the dialog with the given tag if specified
 */
public void show(Fragment fragment, String tag, String replaceTag){
    FragmentManager manager = fragment.getFragmentManager();
    if (manager != null) {
        Fragment existing = manager.findFragmentByTag(replaceTag);
        if (existing != null){
            FragmentManager otherManager = existing.getFragmentManager();
            if (otherManager != null) {
                FragmentTransaction ft = otherManager.beginTransaction();
                ft.remove(existing);
                ft.commit();
            }
        }
        setTargetFragment(fragment, -1);
        try {
            super.show(manager, tag);
        } catch (IllegalStateException ignored) {
        }
    }
}
 
Example 3
Source File: FragmentAdapter.java    From CalendarView with Apache License 2.0 6 votes vote down vote up
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    if (mUpdateFlag) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        String tag = fragment.getTag();
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        transaction.remove(fragment);
        fragment = getItem(position);
        if (!fragment.isAdded()) {
            transaction.add(container.getId(), fragment, tag)
                    .attach(fragment)
                    .commitAllowingStateLoss();
        }
        return fragment;
    }
    return super.instantiateItem(container, position);
}
 
Example 4
Source File: EditorActivity.java    From PHONK with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Toggle API drawer
 */
public void showAPIDrawer(boolean b) {

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_out_left,
            R.anim.slide_out_left);

    if (b) {
        webviewFragment = new APIWebviewFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url", "http://localhost:8585/reference.html");
        webviewFragment.setArguments(bundle);
        ft.add(R.id.fragmentWebview, webviewFragment).addToBackStack(null);

        editorFragment.getView().animate().translationX(-50).setDuration(500).start();
    } else {
        editorFragment.getView().animate().translationX(0).setDuration(500).start();
        ft.remove(webviewFragment);
    }

    ft.commit();
}
 
Example 5
Source File: SimpleDialog.java    From SimpleDialogFragments with Apache License 2.0 6 votes vote down vote up
/**
 * Shows the dialog. Results will be forwarded to the activity supplied.
 * The tag can be used to identify the dialog in {@link OnDialogResultListener#onResult}
 * An optional argument can be used to remove a previously shown dialog with the tag given
 * prior to showing this one.
 *
 * @param activity the hosting activity
 * @param tag the dialogs tag
 * @param replaceTag removes the dialog with the given tag if specified
 */
public void show(FragmentActivity activity, String tag, String replaceTag){
    FragmentManager manager = activity.getSupportFragmentManager();
    Fragment existing = manager.findFragmentByTag(replaceTag);
    if (existing != null) {
        FragmentManager otherManager = existing.getFragmentManager();
        if (otherManager != null) {
            FragmentTransaction ft = otherManager.beginTransaction();
            ft.remove(existing);
            ft.commit();
        }
    }
    try {
        super.show(manager, tag);
    } catch (IllegalStateException ignored) {
    }
}
 
Example 6
Source File: SimpleDialog.java    From SimpleDialogFragments with Apache License 2.0 6 votes vote down vote up
/**
 * Shows the dialog. Results will be forwarded to the activity supplied.
 * The tag can be used to identify the dialog in {@link OnDialogResultListener#onResult}
 * An optional argument can be used to remove a previously shown dialog with the tag given
 * prior to showing this one.
 *
 * @param activity the hosting activity
 * @param tag the dialogs tag
 * @param replaceTag removes the dialog with the given tag if specified
 */
public void show(FragmentActivity activity, String tag, String replaceTag){
    FragmentManager manager = activity.getSupportFragmentManager();
    Fragment existing = manager.findFragmentByTag(replaceTag);
    if (existing != null) {
        FragmentManager otherManager = existing.getFragmentManager();
        if (otherManager != null) {
            FragmentTransaction ft = otherManager.beginTransaction();
            ft.remove(existing);
            ft.commit();
        }
    }
    try {
        super.show(manager, tag);
    } catch (IllegalStateException ignored) {
    }
}
 
Example 7
Source File: SettingsActivity.java    From APDE with GNU General Public License v2.0 6 votes vote down vote up
protected void addFirstSettingsFragment() {
	Bundle args = new Bundle(1);
	args.putString("resource", getResources().getStringArray(R.array.settings_headers_fragments)[0]);
	
	Fragment existingFragment = getSupportFragmentManager().findFragmentById(R.id.settings_fragment_container);
	
	StockPreferenceFragment newFragment = new StockPreferenceFragment();
	newFragment.setArguments(args);
	
	FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
	
	if (existingFragment != null) {
		transaction.remove(existingFragment);
	}
	
	transaction.add(R.id.settings_fragment_container, newFragment);
	transaction.commit();
}
 
Example 8
Source File: InAppBillingFragment.java    From candybar with Apache License 2.0 5 votes vote down vote up
public static void showInAppBillingDialog(@NonNull FragmentManager fm,
                                          int type, @NonNull String key, @NonNull String[] productId,
                                          int[] productCount) {
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag(TAG);
    if (prev != null) {
        ft.remove(prev);
    }

    try {
        DialogFragment dialog = InAppBillingFragment.newInstance(type, key, productId, productCount);
        dialog.show(ft, TAG);
    } catch (IllegalArgumentException | IllegalStateException ignored) {
    }
}
 
Example 9
Source File: StartConversationActivity.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("InflateParams")
protected void showJoinConferenceDialog(final String prefilledJid) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment prev = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG_DIALOG);
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);
    JoinConferenceDialog joinConferenceFragment = JoinConferenceDialog.newInstance(prefilledJid, mActivatedAccounts, xmppConnectionService.multipleAccounts());
    joinConferenceFragment.show(ft, FRAGMENT_TAG_DIALOG);
}
 
Example 10
Source File: SettingsActivity.java    From APDE with GNU General Public License v2.0 5 votes vote down vote up
protected void addHeadersFragment() {
	Fragment existingFragment = getSupportFragmentManager().findFragmentById(R.id.settings_fragment_container);
	
	SettingsHeadersFragment newFragment = new SettingsHeadersFragment();
	FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
	transaction.add(isMultiPane() ? R.id.settings_list_fragment_container : R.id.settings_fragment_container, newFragment);
	
	if (!isMultiPane() && existingFragment != null) {
		transaction.remove(existingFragment);
	}
	
	transaction.commit();
}
 
Example 11
Source File: ActivityUtils.java    From NGA-CLIENT-VER-OPEN-SOURCE with GNU General Public License v2.0 5 votes vote down vote up
private void notice(String title, String content, Context c) {

        if (c == null)
            return;
        NLog.d(TAG, "saying dialog");
        Bundle b = new Bundle();
        b.putString("title", title);
        b.putString("content", content);
        synchronized (lock) {
            try {

                DialogFragment df = new SayingDialogFragment();
                df.setArguments(b);

                FragmentActivity fa = (FragmentActivity) c;
                FragmentManager fm = fa.getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();

                Fragment prev = fm.findFragmentByTag(dialogTag);
                if (prev != null) {
                    ft.remove(prev);
                }

                ft.commit();
                df.show(fm, dialogTag);
                this.df = df;
            } catch (Exception e) {
                NLog.e(this.getClass().getSimpleName(), NLog.getStackTraceString(e));

            }

        }

    }
 
Example 12
Source File: PrivacyPolicyFragment.java    From mimi-reader with Apache License 2.0 5 votes vote down vote up
/**
 * Builds and displays a licenses fragment for you. Requires "/res/raw/licenses.html" and
 * "/res/layout/licenses_fragment.xml" to be present.
 *
 * @param fm A fragment manager instance used to display this LicensesFragment.
 */
public static void displayLicensesFragment(FragmentManager fm) {
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag(FRAGMENT_TAG);
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    DialogFragment newFragment = PrivacyPolicyFragment.newInstance();
    newFragment.show(ft, FRAGMENT_TAG);
}
 
Example 13
Source File: AppRunnerActivity.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
private void removeDebugFragment() {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.remove(mDebugFragment);
    ft.commit();

    debugFramentIsVisible = false;
}
 
Example 14
Source File: FragmentManagerEnhanced.java    From bitmask_android with GNU General Public License v3.0 5 votes vote down vote up
public FragmentTransaction removePreviousFragment(String tag) {
    FragmentTransaction transaction = genericFragmentManager.beginTransaction();
    Fragment previousFragment = genericFragmentManager.findFragmentByTag(tag);
    if (previousFragment != null) {
        transaction.remove(previousFragment);
    }
    transaction.addToBackStack(null);

    return transaction;
}
 
Example 15
Source File: MainActivity.java    From PretendYoureXyzzyAndroid with GNU General Public License v3.0 5 votes vote down vote up
private void addOrReplace(@NonNull FragmentTransaction transaction, @NonNull Fragment newFragment, @NonNull Item item) {
    FragmentManager manager = getSupportFragmentManager();
    Fragment fragment = manager.findFragmentByTag(item.tag);
    if (fragment != null) transaction.remove(fragment);

    transaction.add(R.id.main_container, newFragment, item.tag);
}
 
Example 16
Source File: NavigationActivity.java    From MTweaks-KernelAdiutorMOD with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void finish() {
    super.finish();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    for (int id : mActualFragments.keySet()) {
        Fragment fragment = fragmentManager.findFragmentByTag(id + "_key");
        if (fragment != null) {
            fragmentTransaction.remove(fragment);
        }
    }
    fragmentTransaction.commitAllowingStateLoss();
    RootUtils.closeSU();
}
 
Example 17
Source File: BlocklistActivity.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
protected void showEnterJidDialog() {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);
    EnterJidDialog dialog = EnterJidDialog.newInstance(
            null,
            getString(R.string.block_jabber_id),
            getString(R.string.block),
            null,
            account.getJid().asBareJid().toEscapedString(),
            true,
            xmppConnectionService.multipleAccounts(),
            false
    );

    dialog.setOnEnterJidDialogPositiveListener((accountJid, contactJid) -> {
        Blockable blockable = new RawBlockable(account, contactJid);
        if (xmppConnectionService.sendBlockRequest(blockable, false)) {
            ToastCompat.makeText(BlocklistActivity.this, R.string.corresponding_conversations_closed, Toast.LENGTH_SHORT).show();
        }
        return true;
    });
    dialog.show(ft, "dialog");
}
 
Example 18
Source File: PreferenceActivity.java    From AndroidPreferenceActivity with Apache License 2.0 5 votes vote down vote up
/**
 * Shows a specific preference fragment.
 *
 * @param navigationPreference
 *         The navigation preference, the fragment, which should be shown, is associated with,
 *         as an instance of the class {@link NavigationPreference}. The navigation preference
 *         may not be null
 * @param fragment
 *         The fragment, which should be shown, as an instance of the class Fragment. The
 *         fragment may not be null
 */
private void showPreferenceFragment(@NonNull final NavigationPreference navigationPreference,
                                    @NonNull final Fragment fragment) {
    fragment.setRetainInstance(true);
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    if (!isSplitScreen()) {
        transaction.hide(navigationFragment);

        if (preferenceFragment != null) {
            transaction.remove(preferenceFragment);
            notifyOnPreferenceFragmentHidden(preferenceFragment);
        }

        transaction.add(R.id.navigation_fragment_container, fragment, PREFERENCE_FRAGMENT_TAG);
    } else {
        if (preferenceFragment != null) {
            notifyOnPreferenceFragmentHidden(preferenceFragment);
        }

        transaction
                .replace(R.id.preference_fragment_container, fragment, PREFERENCE_FRAGMENT_TAG);
    }

    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    transaction.commit();
    this.preferenceFragment = fragment;
    showToolbarNavigationIcon();
    adaptBreadCrumbVisibility(selectedPreferenceFragmentArguments);
    notifyOnPreferenceFragmentShown(navigationPreference, fragment);
}
 
Example 19
Source File: StageActivity.java    From MHViewer with Apache License 2.0 4 votes vote down vote up
private void finishScene(String tag, TransitionHelper transitionHelper) {
    FragmentManager fragmentManager = getSupportFragmentManager();

    // Get scene
    Fragment scene = fragmentManager.findFragmentByTag(tag);
    if (scene == null) {
        Log.e(TAG, "finishScene: Can't find scene by tag: " + tag);
        return;
    }

    // Get scene index
    int index = mSceneTagList.indexOf(tag);
    if (index < 0) {
        Log.e(TAG, "finishScene: Can't find the tag in tag list: " + tag);
        return;
    }

    if (mSceneTagList.size() == 1) {
        // It is the last fragment, finish Activity now
        Log.i(TAG, "finishScene: It is the last scene, finish activity now");
        finish();
        return;
    }

    Fragment next = null;
    if (index == mSceneTagList.size() - 1) {
        // It is first fragment, show the next one
        next = fragmentManager.findFragmentByTag(mSceneTagList.get(index - 1));
    }

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    if (next != null) {
        if (transitionHelper == null || !transitionHelper.onTransition(
                this, transaction, scene, next)) {
            // Clear shared item
            scene.setSharedElementEnterTransition(null);
            scene.setSharedElementReturnTransition(null);
            scene.setEnterTransition(null);
            scene.setExitTransition(null);
            next.setSharedElementEnterTransition(null);
            next.setSharedElementReturnTransition(null);
            next.setEnterTransition(null);
            next.setExitTransition(null);
            // Do not show animate if it is not the first fragment
            transaction.setCustomAnimations(R.anim.scene_close_enter, R.anim.scene_close_exit);
        }
        // Attach fragment
        transaction.attach(next);
    }
    transaction.remove(scene);
    transaction.commitAllowingStateLoss();
    onTransactScene();

    // Remove tag
    mSceneTagList.remove(index);

    // Return result
    if (scene instanceof SceneFragment) {
        ((SceneFragment) scene).returnResult(this);
    }
}
 
Example 20
Source File: ActivityUtils.java    From NGA-CLIENT-VER-OPEN-SOURCE with GNU General Public License v2.0 3 votes vote down vote up
public void dismiss() {

        synchronized (lock) {
            NLog.d(TAG, "trying dissmiss dialog");


            if (df != null && df.getActivity() != null) {
                NLog.d(TAG, "dissmiss dialog");

                try {
                    FragmentActivity fa = (FragmentActivity) (df.getActivity());
                    FragmentManager fm = fa.getSupportFragmentManager();
                    FragmentTransaction ft = fm.beginTransaction();

                    Fragment prev = fm.findFragmentByTag(dialogTag);
                    if (prev != null) {
                        ft.remove(prev);

                    }

                    ft.commit();
                } catch (Exception e) {
                    NLog.e(this.getClass().getSimpleName(), NLog.getStackTraceString(e));
                }

                df = null;


            } else {
                df = null;
            }

        }
    }