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

The following examples show how to use androidx.fragment.app.FragmentTransaction#commitAllowingStateLoss() . 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: ContactSelectionActivity.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void addFragment(FragmentActivity fragmentActivity, Fragment fragmentToAdd,
                               String fragmentTag) {
    FragmentManager supportFragmentManager = fragmentActivity.getSupportFragmentManager();

    FragmentTransaction fragmentTransaction = supportFragmentManager
            .beginTransaction();
    fragmentTransaction.replace(R.id.layout_child_activity, fragmentToAdd,
            fragmentTag);

    if (supportFragmentManager.getBackStackEntryCount() > 1) {
        supportFragmentManager.popBackStack();
    }
    fragmentTransaction.addToBackStack(fragmentTag);
    fragmentTransaction.commitAllowingStateLoss();
    supportFragmentManager.executePendingTransactions();
}
 
Example 2
Source File: ManageDevicesActivity.java    From science-journal with Apache License 2.0 6 votes vote down vote up
private void setupFragment() {
  FragmentManager fragmentManager = getSupportFragmentManager();
  Fragment fragmentById = fragmentManager.findFragmentById(R.id.fragment);
  if (fragmentById != null) {
    manageFragment = (ManageDevicesRecyclerFragment) fragmentById;
  } else {
    manageFragment = new ManageDevicesRecyclerFragment();
    Bundle args = new Bundle();
    args.putString(EXTRA_ACCOUNT_KEY, getIntent().getStringExtra(EXTRA_ACCOUNT_KEY));
    args.putString(EXTRA_EXPERIMENT_ID, getIntent().getStringExtra(EXTRA_EXPERIMENT_ID));
    manageFragment.setArguments(args);
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.replace(R.id.fragment, manageFragment);
    ft.commitAllowingStateLoss();
  }
}
 
Example 3
Source File: FragmentUtility.java    From natrium-android-wallet with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Performs a fragment addition transaction with all of the necessary animations and tags.
 */
public void performAddTransaction(Fragment fragment,
                                  Animation pushAnimations,
                                  Animation popAnimations,
                                  @Nullable String tag,
                                  boolean addToBackStack,
                                  View sharedElement) {
    FragmentTransaction transaction = mFragmentManager.beginTransaction()
            .setCustomAnimations(
                    pushAnimations.getEnter(),
                    popAnimations.getExit(),
                    popAnimations.getEnter(),
                    pushAnimations.getExit()
            )
            .replace(mContainerViewId, fragment, tag);

    if (addToBackStack) {
        transaction.addToBackStack(tag);
    }

    transaction.commitAllowingStateLoss();
}
 
Example 4
Source File: StageActivity.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
public void refreshTopScene() {
    int index = mSceneTagList.size() - 1;
    if (index < 0) {
        return;
    }
    String tag = mSceneTagList.get(index);

    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentByTag(tag);

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.detach(fragment);
    transaction.attach(fragment);
    transaction.commitAllowingStateLoss();
    onTransactScene();
}
 
Example 5
Source File: MainActivity.java    From QuickDevFramework with Apache License 2.0 6 votes vote down vote up
private void switchFragment(String tag) {
    /* Fragment 切换 */
    FragmentTransaction transaction = mFragmentManager.beginTransaction();
    if (fragmentTags.indexOf(tag) < 0) {
        return;
    }
    Fragment showFragment = fragments.get(fragmentTags.indexOf(tag));
    Fragment currentFragment = mFragmentManager.findFragmentByTag(currentTag);
    if (currentFragment != null) {
        transaction.hide(currentFragment);
    }
    if (showFragment.isAdded()) {
        transaction.show(showFragment);
    }
    else {
        transaction.add(R.id.content_frame, showFragment, tag);
        transaction.show(showFragment);
    }
    transaction.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.commitAllowingStateLoss();
    currentTag = tag;
}
 
Example 6
Source File: FragmentController.java    From arcusandroid with Apache License 2.0 5 votes vote down vote up
public void replaceAfterInitial(@NonNull Fragment fragment) {
    try {
        while (fragmentManager.getBackStackEntryCount() != 1) {
            fragmentManager.popBackStackImmediate();
        }

        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.container, fragment);
        transaction.addToBackStack(fragment.getClass().getName());
        transaction.commitAllowingStateLoss();
    } catch (Exception e) {
        logger.error("An error occurred replacing fragment after initial.", e);
    }
}
 
Example 7
Source File: NavigationActivity.java    From SmartPack-Kernel-Manager 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 8
Source File: FragmentDemo.java    From ShineButton with MIT License 5 votes vote down vote up
public void showFragment(final FragmentManager fragmentManager) {
    this.fragmentManager = fragmentManager;
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.setCustomAnimations(
            R.anim.fragmentv_slide_bottom_enter,
            0,
            0,
            R.anim.fragmentv_slide_top_exit);
    transaction.add(Window.ID_ANDROID_CONTENT, FragmentDemo.this, "FragmentDemo");
    transaction.addToBackStack(null);
    transaction.commitAllowingStateLoss();
}
 
Example 9
Source File: MobiComKitPeopleActivity.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void addFragment(FragmentActivity fragmentActivity, Fragment fragmentToAdd, String fragmentTag) {
    FragmentManager supportFragmentManager = fragmentActivity.getSupportFragmentManager();

    FragmentTransaction fragmentTransaction = supportFragmentManager
            .beginTransaction();
    fragmentTransaction.replace(R.id.layout_child_activity, fragmentToAdd,
            fragmentTag);

    if (supportFragmentManager.getBackStackEntryCount() > 1) {
        supportFragmentManager.popBackStack();
    }
    fragmentTransaction.addToBackStack(fragmentTag);
    fragmentTransaction.commitAllowingStateLoss();
    supportFragmentManager.executePendingTransactions();
}
 
Example 10
Source File: FragmentLifecycleActivity.java    From AndroidAll with Apache License 2.0 5 votes vote down vote up
public void showFragment(@NonNull Fragment fragment) {
    //相当于new一个实例FragmentTransaction
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    if (fragment.isAdded()) {
        ft.show(fragment);
    } else {
        //把该ft加入到栈中进行管理
        ft.addToBackStack(fragment.getClass().getSimpleName());
        ft.add(R.id.container, fragment);
    }
    ft.commitAllowingStateLoss();
    Log.e("Lifecycle", "getBackStackEntryCount: " + getSupportFragmentManager().getBackStackEntryCount());
}
 
Example 11
Source File: FragmentUtility.java    From natrium-android-wallet with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Performs a fragment replacement transaction with all of the necessary animations and tags.
 */
public void performReplaceTransaction(Fragment fragment,
                                      Animation pushAnimations,
                                      Animation popAnimations,
                                      @Nullable String tag,
                                      boolean addToBackStack) {
    FragmentTransaction transaction;
    if (tag == null) {
        transaction = mFragmentManager.beginTransaction()
                .setCustomAnimations(
                        pushAnimations.getEnter(),
                        popAnimations.getExit(),
                        popAnimations.getEnter(),
                        pushAnimations.getExit()
                )
                .replace(mContainerViewId, fragment);
    } else {
        transaction = mFragmentManager.beginTransaction()
                .setCustomAnimations(
                        pushAnimations.getEnter(),
                        popAnimations.getExit(),
                        popAnimations.getEnter(),
                        pushAnimations.getExit()
                )
                .replace(mContainerViewId, fragment, tag);
    }

    if (addToBackStack) {
        transaction.addToBackStack(tag);
    }

    transaction.commitAllowingStateLoss();
}
 
Example 12
Source File: ExtendedDialogFragment.java    From InviZible with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void show(FragmentManager manager, String tag) {
    try {
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commitAllowingStateLoss();
    } catch (IllegalStateException e) {
        Log.w(LOG_TAG, "ExtendedDialogFragment Exception " + e.getMessage() + " " + e.getCause());
    }
}
 
Example 13
Source File: MainActivity.java    From GifView with Apache License 2.0 5 votes vote down vote up
public void openFragment() {
    FragmentTransaction trans = getSupportFragmentManager()
            .beginTransaction();
    GifFragment fragmentLocal =  GifFragment.newInstance();
    fragmentLocal.setHasOptionsMenu(true);
    trans.replace(R.id.frame, fragmentLocal, fragmentLocal.getTAG());
    trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    trans.addToBackStack(fragmentLocal.getTAG());
    trans.commitAllowingStateLoss();
}
 
Example 14
Source File: Themes.java    From FragmentMaster with Apache License 2.0 5 votes vote down vote up
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    FragmentManager fragmentManager = getChildFragmentManager();
    if (fragmentManager.findFragmentByTag("TAG_CHILD") == null) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.add(R.id.childContainer, new DarkThemeChildPage(), "TAG_CHILD");
        ft.commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    }
}
 
Example 15
Source File: CandyBarMainActivity.java    From candybar with Apache License 2.0 5 votes vote down vote up
private void setFragment(Fragment fragment) {
    clearBackStack();

    FragmentTransaction ft = mFragManager.beginTransaction()
            .replace(R.id.container, fragment, mFragmentTag);
    try {
        ft.commit();
    } catch (Exception e) {
        ft.commitAllowingStateLoss();
    }

    Menu menu = mNavigationView.getMenu();
    menu.getItem(mPosition).setChecked(true);
    mToolbarTitle.setText(menu.getItem(mPosition).getTitle());
}
 
Example 16
Source File: BaseDialogFragment.java    From android-styled-dialogs with Apache License 2.0 4 votes vote down vote up
public void showAllowingStateLoss(FragmentManager manager, String tag) {
    FragmentTransaction ft = manager.beginTransaction();
    ft.add(this, tag);
    ft.commitAllowingStateLoss();
}
 
Example 17
Source File: RegistrationActivity.java    From ridesharing-android with MIT License 4 votes vote down vote up
private void addFragment(Fragment fragment) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_frame, fragment);
    transaction.commitAllowingStateLoss();
}
 
Example 18
Source File: LoginActivity.java    From tindroid with Apache License 2.0 4 votes vote down vote up
private void showFragment(String tag, Bundle args, Boolean addToBackstack) {
    if (isFinishing() || isDestroyed()) {
        return;
    }

    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentByTag(tag);
    if (fragment == null) {
        switch (tag) {
            case FRAGMENT_LOGIN:
                fragment = new LoginFragment();
                break;
            case FRAGMENT_SETTINGS:
                fragment = new LoginSettingsFragment();
                break;
            case FRAGMENT_SIGNUP:
                fragment = new SignUpFragment();
                break;
            case FRAGMENT_RESET:
                fragment = new PasswordResetFragment();
                break;
            case FRAGMENT_CREDENTIALS:
                fragment = new CredentialsFragment();
                break;
            default:
                throw new IllegalArgumentException();
        }
    }

    if (args != null) {
        fragment.setArguments(args);
    }

    FragmentTransaction tx = fm.beginTransaction()
            .replace(R.id.contentFragment, fragment)
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    if (addToBackstack) {
        tx = tx.addToBackStack(null);
    }
    tx.commitAllowingStateLoss();
}
 
Example 19
Source File: StageActivity.java    From EhViewer 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: SetupWizardActivity.java    From talkback with Apache License 2.0 4 votes vote down vote up
private void displayScreen(SetupScreen screen) {
  FragmentManager fragmentManager = getSupportFragmentManager();
  /* Hide the previous fragment. */
  if (currentScreenFragment != null) {
    /* In order to ensure that state information from the previous fragment has been saved, hide
     * the previous fragment with a separate fragment transaction than the one used to show the
     * new fragment. */
    fragmentManager
        .beginTransaction()
        .hide(currentScreenFragment)
        .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
        .commitAllowingStateLoss();
    fragmentManager.executePendingTransactions();
  }

  SetupWizardScreenFragment fragment =
      (SetupWizardScreenFragment) fragmentManager.findFragmentByTag(screen.name());
  FragmentTransaction transaction = fragmentManager.beginTransaction();

  transaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
  if (fragment == null) {
    /* Create a new fragment. */
    fragment = createScreenFragment(screen);
    if (currentScreenFragment != null) {
      transaction.add(R.id.fragment_layout_container, fragment, screen.name());
    } else {
      transaction.replace(R.id.fragment_layout_container, fragment, screen.name());
    }
  } else {
    /* The desired fragment has been previously created, so show it. */
    transaction.show(fragment);
  }

  /* Update the view to reflect the new screen. */
  transaction.commitAllowingStateLoss();
  currentScreenFragment = fragment;
  currentSetupScreen = screen;
  setNavigationButtonText(currentScreenFragment);

  if (screenViewListener != null) {
    screenViewListener.onScreenShown(currentScreenFragment.getScreenName());
  }

  if (setupScreenListener != null) {
    setupScreenListener.onSetupScreenShown(currentSetupScreen);
  }
}