下面列出了androidx.fragment.app.FragmentTransaction#remove ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
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();
}
/**
* 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) {
}
}
/**
* 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) {
}
}
/**
* 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();
}
@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);
}
/**
* 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) {
}
}
}
/**
* 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);
}
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;
}
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) {
}
}
@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);
}
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();
}
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));
}
}
}
/**
* 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);
}
private void removeDebugFragment() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.remove(mDebugFragment);
ft.commit();
debugFramentIsVisible = false;
}
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);
}
@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();
}
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");
}
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);
}
}
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;
}
}
}