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

The following examples show how to use androidx.fragment.app.FragmentTransaction#addToBackStack() . 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: AvatarSelectionActivity.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onCameraSelected() {
  if (isCameraFirst() && popToRoot()) {
    return;
  }

  Fragment            fragment    = CameraFragment.newInstanceForAvatarCapture();
  FragmentTransaction transaction = getSupportFragmentManager().beginTransaction()
                                                               .replace(R.id.fragment_container, fragment, IMAGE_CAPTURE);

  if (isGalleryFirst()) {
    transaction.addToBackStack(null);
  }

  transaction.commit();
}
 
Example 2
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 3
Source File: FragmentController.java    From arcusandroid with Apache License 2.0 6 votes vote down vote up
public void navigateToFragment(Fragment fragment, String tag, boolean addToBackStack, TransitionEffect animation, int containerResId) {

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        if (animation != null && animation.isAnimation()) {
            transaction.setCustomAnimations(animation.enter, animation.exit, animation.popEnter, animation.popExit);
        }

        if (animation != null && animation.isTransition()) {
            transaction.setTransition(animation.transitionId);
        }

        transaction.replace(containerResId, fragment, fragment.getClass().getName());
        if (addToBackStack)
            transaction.addToBackStack(tag);

        try {
            transaction.commitAllowingStateLoss();
        } catch (Exception e) {
            logger.error("An error occurred while navigating to fragment.", e);
        }
    }
 
Example 4
Source File: MainActivity.java    From Alarmio with Apache License 2.0 6 votes vote down vote up
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (isActionableIntent(intent)) {
        FragmentManager manager = getSupportFragmentManager();
        BaseFragment newFragment = createFragmentFor(intent);
        BaseFragment fragment = fragmentRef != null ? fragmentRef.get() : null;

        if (newFragment == null || newFragment.equals(fragment)) // check that fragment isn't already displayed
            return;

        if (newFragment instanceof HomeFragment && manager.getBackStackEntryCount() > 0) // clear the back stack
            manager.popBackStack(manager.getBackStackEntryAt(0).getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);

        FragmentTransaction transaction = manager.beginTransaction()
                .setCustomAnimations(R.anim.slide_in_up_sheet, R.anim.slide_out_up_sheet, R.anim.slide_in_down_sheet, R.anim.slide_out_down_sheet)
                .replace(R.id.fragment, newFragment);

        if (fragment instanceof HomeFragment && !(newFragment instanceof HomeFragment))
            transaction.addToBackStack(null);

        fragmentRef = new WeakReference<>(newFragment);
        transaction.commit();
    }
}
 
Example 5
Source File: MainActivity.java    From ui with Apache License 2.0 6 votes vote down vote up
@Override
public void onFragmentInteraction(int which) {
    //going to change via the transaction manager, instead of just a simple replace.
    FragmentTransaction transaction = fragmentManager.beginTransaction();

    //remove the current fragment...
    //transaction.remove(fragmentManager.findFragmentById(R.id.container));
    if (which == 1) { //first fragment
        //replace with first fragment
        transaction.replace(R.id.container, FirstFragment.newInstance(String.valueOf(num_one), "Called From MainFrag"));
        num_one++;
        whichfragment = 1;
    } else { //must be 2 (hopefully!)
        //replace with first fragment
        transaction.replace(R.id.container, SecondFragment.newInstance(String.valueOf(num_two), "Called From MainFrag"));
        num_two++;
        whichfragment = 2;
    }
    // and add the transaction to the back stack so the user can navigate back
    transaction.addToBackStack(null);

    // Commit the transaction
    transaction.commit();
}
 
Example 6
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 7
Source File: OdysseyMainActivity.java    From odyssey with GNU General Public License v3.0 6 votes vote down vote up
private void onDirectorySelected(final String dirPath, final boolean isRootDirectory, final boolean addToBackStack) {
    // Create fragment and give it an argument for the selected directory
    final FilesFragment newFragment = FilesFragment.newInstance(dirPath, isRootDirectory);

    final FragmentManager fragmentManager = getSupportFragmentManager();

    final FragmentTransaction transaction = fragmentManager.beginTransaction();

    if (!isRootDirectory) {
        // no root directory so set a enter / exit transition
        final int layoutDirection = getResources().getConfiguration().getLayoutDirection();
        newFragment.setEnterTransition(new Slide(GravityCompat.getAbsoluteGravity(GravityCompat.START, layoutDirection)));
        newFragment.setExitTransition(new Slide(GravityCompat.getAbsoluteGravity(GravityCompat.END, layoutDirection)));
    }

    transaction.replace(R.id.fragment_container, newFragment);
    if (!isRootDirectory && addToBackStack) {
        // add fragment only to the backstack if it's not a root directory
        transaction.addToBackStack("FilesFragment");
    }

    // Commit the transaction
    transaction.commit();
}
 
Example 8
Source File: OdysseyMainActivity.java    From odyssey with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onAlbumSelected(AlbumModel album, Bitmap bitmap) {
    // Create fragment and give it an argument for the selected article
    AlbumTracksFragment newFragment = AlbumTracksFragment.newInstance(album, bitmap);

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    // set enter / exit animation
    newFragment.setEnterTransition(new Slide(Gravity.BOTTOM));
    newFragment.setExitTransition(new Slide(Gravity.TOP));

    // Replace whatever is in the fragment_container view with this
    // fragment,
    // and add the transaction to the back stack so the user can navigate
    // back
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack("AlbumTracksFragment");

    // Commit the transaction
    transaction.commit();
}
 
Example 9
Source File: ControllerPadFragment.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    FragmentActivity activity = getActivity();

    switch (item.getItemId()) {
        case R.id.action_help:
            if (activity != null) {
                FragmentManager fragmentManager = activity.getSupportFragmentManager();
                if (fragmentManager != null) {
                    CommonHelpFragment helpFragment = CommonHelpFragment.newInstance(getString(R.string.controlpad_help_title), getString(R.string.controlpad_help_text));
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction()
                            .replace(R.id.contentLayout, helpFragment, "Help");
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();
                }
            }
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}
 
Example 10
Source File: MainActivity.java    From AndroidApp with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Open {@link EditPoiFragment} to add more info o POi
 *
 * @param poi {@link ParcelablePOI} to edit
 */
public void editPoi(ParcelablePOI poi) {
    //check authentication before editing
    if (!checkAuthentication()) return;
    //close PoiListFragment if it is open
    PoiListFragment poiListFragment = (PoiListFragment) getSupportFragmentManager().findFragmentByTag("PoiListFragment");
    if (poiListFragment != null)
        getSupportFragmentManager()
                .beginTransaction()
                .remove(poiListFragment)
                .commit();

    //if MapFragment is open close any routing
    MapFragment MapFragment = (MapFragment) getSupportFragmentManager().findFragmentByTag("MapFragment");
    if (MapFragment != null) {
        MapFragment.clearMapDisplay();
    }

    Fragment fragment = EditPoiFragment.newInstance(poi);
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.container, fragment, "EditPoiFragment");// give your fragment container id in first parameter
    transaction.addToBackStack(null);  // if written, this transaction will be added to backstack
    transaction.commit();
    hideNavigation();
}
 
Example 11
Source File: MainActivity.java    From ArchPackages with GNU General Public License v3.0 6 votes vote down vote up
/**
 * callback from {@link DetailsFragment}
 * inflate a new {@link ResultFragment}
 * with keywords parameter by exact name
 *
 * @param packageName: the dependency clicked
 */
@Override
public void onDetailsFragmentCallbackOnPackageClicked(String packageName) {
    if (packageName != null && !TextUtils.isEmpty(packageName)) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        ResultFragment resultFragment = ResultFragment.newInstance(
                ArchPackagesConstants.SEARCH_KEYWORDS_PARAMETER_EXACT_NAME,
                packageName,
                null,
                null,
                null);
        fragmentTransaction.add(R.id.content_main_fragment_container, resultFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}
 
Example 12
Source File: MainActivity.java    From AsteroidOSSync with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onAppSettingsClicked() {
    Fragment f = new AppListFragment();

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    if (mDetailFragment != null) {
        mPreviousFragment = mDetailFragment;
        mDetailFragment = null;
    }
    if (mListFragment != null) {
        mPreviousFragment = mListFragment;
        mListFragment = null;
    }
    ft.replace(R.id.flContainer, f);
    ft.addToBackStack(null);
    ft.commit();

    setTitle(getString(R.string.notifications_settings));
    ActionBar ab = getSupportActionBar();
    if (ab != null)
        ab.setDisplayHomeAsUpEnabled(true);
}
 
Example 13
Source File: ThetaFeatureActivity.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * Fragment の遷移.
 * @param f Fragment
 */
private void moveFragment(final Fragment f) {
    FragmentTransaction t = getSupportFragmentManager().beginTransaction();
    t.setTransition(FragmentTransaction.TRANSIT_NONE);
    t.replace(android.R.id.content, f);
    t.addToBackStack(null);
    t.commit();

}
 
Example 14
Source File: MainActivity.java    From ArchPackages with GNU General Public License v3.0 5 votes vote down vote up
/**
 * callback from {@link DetailsFragment}
 * inflate a new {@link FilesFragment}
 *
 * @param files: the {@link Files} to bind dirs and files count
 */
@Override
public void onDetailsFragmentCallbackOnFilesClicked(Files files) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    FilesFragment filesFragment = FilesFragment.newInstance(files);
    fragmentTransaction.add(R.id.content_main_fragment_container, filesFragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}
 
Example 15
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 16
Source File: ViewUtils.java    From SmartPack-Kernel-Manager with GNU General Public License v3.0 5 votes vote down vote up
public static void showDialog(FragmentManager manager, DialogFragment fragment) {
    FragmentTransaction ft = manager.beginTransaction();
    Fragment prev = manager.findFragmentByTag("dialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    fragment.show(ft, "dialog");
}
 
Example 17
Source File: MainActivity.java    From ui with Apache License 2.0 5 votes vote down vote up
@Override
public void onFragmentInteraction1(String Data) {

    //now change to the SecondFragment, pressing the back button should go to main fragment.
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    //remove firstfragment from the stack and replace it with two.
    transaction.replace(R.id.container, SecondFragment.newInstance(String.valueOf(num_two), Data));
    // and add the transaction to the back stack so the user can navigate back
    transaction.addToBackStack(null);

    // Commit the transaction
    transaction.commit();

    num_two++;
}
 
Example 18
Source File: MainActivity.java    From guanggoo-android with Apache License 2.0 5 votes vote down vote up
public static void addFragmentToStack(FragmentManager fm, Fragment fragment) {
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.fragment_container, fragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(sStackName);
    ft.commit();
}
 
Example 19
Source File: BaseActivity.java    From android-drag-FlowLayout with Apache License 2.0 5 votes vote down vote up
protected void replaceFragment(int containerViewId, Fragment fragment, boolean addToback) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction().replace(containerViewId, fragment);
    if (addToback) {
        ft.addToBackStack(fragment.getClass().getName());
    }
    ft.commit();
}
 
Example 20
Source File: StartConversationActivity.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
private void showPublicChannelDialog() {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment prev = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG_DIALOG);
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);
    CreatePublicChannelDialog dialog = CreatePublicChannelDialog.newInstance(mActivatedAccounts, xmppConnectionService.multipleAccounts());
    dialog.show(ft, FRAGMENT_TAG_DIALOG);
}