Java Code Examples for androidx.fragment.app.FragmentManager#getBackStackEntryCount()

The following examples show how to use androidx.fragment.app.FragmentManager#getBackStackEntryCount() . 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: NavigationFragment.java    From AndroidNavigation with MIT License 6 votes vote down vote up
@Override
protected boolean onBackPressed() {
    if (dragging) {
        return true;
    }

    AwesomeFragment topFragment = getTopFragment();
    if (topFragment != null && !topFragment.isBackInteractive()) {
        return true;
    }

    FragmentManager fragmentManager = getChildFragmentManager();
    int count = fragmentManager.getBackStackEntryCount();
    if (count > 1) {
        if (topFragment != null) {
            popFragment();
        }
        return true;
    } else {
        return super.onBackPressed();
    }
}
 
Example 2
Source File: MainActivity.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();

        //Fragment activeFragment = UIService.getActiveFragment(fragmentActivity);
        FragmentTransaction fragmentTransaction = supportFragmentManager
                .beginTransaction();
        /*if (null != activeFragment) {
            fragmentTransaction.hide(activeFragment);
        }*/

        fragmentTransaction.replace(R.id.container, fragmentToAdd,
                fragmentTag);

        if (supportFragmentManager.getBackStackEntryCount() > 1) {
            supportFragmentManager.popBackStack();
        }
        fragmentTransaction.addToBackStack(fragmentTag);
        fragmentTransaction.commit();
        supportFragmentManager.executePendingTransactions();
        //Log.i(TAG, "BackStackEntryCount: " + supportFragmentManager.getBackStackEntryCount());
    }
 
Example 3
Source File: AwesomeActivity.java    From AndroidNavigation with MIT License 6 votes vote down vote up
@Override
public void onBackPressed() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    int count = fragmentManager.getBackStackEntryCount();
    if (count > 0) {
        FragmentManager.BackStackEntry entry = fragmentManager.getBackStackEntryAt(count - 1);
        AwesomeFragment fragment = (AwesomeFragment) fragmentManager.findFragmentByTag(entry.getName());
        if (fragment != null && fragment.isAdded() && !fragment.dispatchBackPressed()) {
            if (count == 1) {
                if (!handleBackPressed()) {
                    ActivityCompat.finishAfterTransition(this);
                }
            } else {
                dismissFragment(fragment);
            }
        }
    } else {
        super.onBackPressed();
    }
}
 
Example 4
Source File: AwesomeActivity.java    From AndroidNavigation with MIT License 6 votes vote down vote up
protected void clearFragmentsInternal() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    int count = fragmentManager.getBackStackEntryCount();
    if (count > 0) {
        getWindow().setBackgroundDrawable(new ColorDrawable(style.getScreenBackgroundColor()));
        String root = fragmentManager.getBackStackEntryAt(0).getName();
        String top = fragmentManager.getBackStackEntryAt(count - 1).getName();
        AwesomeFragment rootFragment = (AwesomeFragment) fragmentManager.findFragmentByTag(root);
        AwesomeFragment topFragment = (AwesomeFragment) fragmentManager.findFragmentByTag(top);
        if (topFragment != null) {
            topFragment.setAnimation(PresentAnimation.Fade);
            fragmentManager.beginTransaction().setMaxLifecycle(topFragment, Lifecycle.State.STARTED).commit();
        }
        if (rootFragment != null) {
            rootFragment.setAnimation(PresentAnimation.Fade);
            fragmentManager.popBackStack(root, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }
}
 
Example 5
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 6
Source File: MainActivity.java    From AsteroidOSSync with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onBackPressed() {
    FragmentManager fm = getSupportFragmentManager();
    if(fm.getBackStackEntryCount() > 0) {
        fm.popBackStack();
        setTitle(mPrefs.getString(PREFS_DEFAULT_LOC_NAME, ""));
        ActionBar ab = getSupportActionBar();
        if (ab != null)
            ab.setDisplayHomeAsUpEnabled(false);
    } else
        finish();
    try {
        mDetailFragment = (DeviceDetailFragment)mPreviousFragment;
    } catch (ClassCastException ignored1) {
        try {
            mListFragment = (DeviceListFragment)mPreviousFragment;
        } catch (ClassCastException ignored2) {}
    }
}
 
Example 7
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 8
Source File: FragmentHelper.java    From AndroidNavigation with MIT License 5 votes vote down vote up
@Nullable
public static AwesomeFragment getAheadFragment(@NonNull FragmentManager fragmentManager, @NonNull AwesomeFragment fragment) {
    int count = fragmentManager.getBackStackEntryCount();
    int index = findIndexAtBackStack(fragmentManager, fragment);
    if (index > 0 && index < count) {
        FragmentManager.BackStackEntry backStackEntry = fragmentManager.getBackStackEntryAt(index - 1);
        AwesomeFragment ahead = (AwesomeFragment) fragmentManager.findFragmentByTag(backStackEntry.getName());
        if (ahead != null && ahead.isAdded()) {
            return ahead;
        }
    }
    return null;
}
 
Example 9
Source File: FleetConnectivityActivity.java    From here-android-sdk-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Pushes the @{link JobDetailsFragment} on the stack.
 *
 * @param jobId ID of the job of which the details should be displayed.
 */
@Override
public void showJobDetails(String jobId) {
    FragmentManager fm = getSupportFragmentManager();
    if (fm.getBackStackEntryCount() > 0 && JobDetailsFragment.TAG.equals(fm.getBackStackEntryAt(fm.getBackStackEntryCount() - 1).getName())) {
        fm.popBackStack();
    }
    FragmentTransaction ft = fm.beginTransaction();
    ft.setCustomAnimations(R.anim.right_enter, R.anim.right_exit, R.anim.right_enter, R.anim.right_exit);
    ft.add(R.id.container, JobDetailsFragment.newInstance(jobId), JobDetailsFragment.TAG);
    ft.addToBackStack(JobDetailsFragment.TAG);
    ft.commit();
}
 
Example 10
Source File: BaseActivity.java    From prayer-times-android with Apache License 2.0 5 votes vote down vote up
public boolean back() {
    FragmentManager fm = getBaseActivity().getSupportFragmentManager();
    if (fm.getBackStackEntryCount() > 0) {
        fm.popBackStack();

        return true;
    }
    return false;
}
 
Example 11
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 12
Source File: BaseActivity.java    From prayer-times-android with Apache License 2.0 5 votes vote down vote up
public void backToMain() {
    FragmentManager fm = getBaseActivity().getSupportFragmentManager();
    int c = fm.getBackStackEntryCount();
    for (int i = 0; i < c; i++) {
        fm.popBackStack();
    }
}
 
Example 13
Source File: FragmentHelper.java    From AndroidNavigation with MIT License 5 votes vote down vote up
public static int findIndexAtBackStack(@NonNull FragmentManager fragmentManager, @NonNull AwesomeFragment fragment) {
    int count = fragmentManager.getBackStackEntryCount();
    int index = -1;
    for (int i = 0; i < count; i++) {
        FragmentManager.BackStackEntry backStackEntry = fragmentManager.getBackStackEntryAt(i);
        String tag = fragment.getTag();
        if (tag != null && tag.equals(backStackEntry.getName())) {
            index = i;
        }
    }
    return index;
}
 
Example 14
Source File: AccessLogActivity.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case android.R.id.home:
            callFragment();
            FragmentManager manager = getSupportFragmentManager();
            if (manager.getBackStackEntryCount() > 0) {
                manager.popBackStack();
            } else {
                finish();
            }
            return true;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 15
Source File: BaseActivity.java    From prayer-times-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onBackStackChanged() {
    FragmentManager fm = getSupportFragmentManager();
    if (fm.getBackStackEntryCount() > 0) {
        mToolbar.setNavigationIcon(
                MaterialDrawableBuilder.with(this).setIcon(MaterialDrawableBuilder.IconValue.ARROW_LEFT).setColorResource(R.color.white)
                        .setToActionbarSize().build());
    } else {
        mToolbar.setNavigationIcon(
                MaterialDrawableBuilder.with(this).setIcon(MaterialDrawableBuilder.IconValue.MENU).setColorResource(R.color.white)
                        .setToActionbarSize().build());
    }
}
 
Example 16
Source File: AccessLogActivity.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * 前の Fragment に戻ります.
 * <p>
 * 前の Fragment がない場合には Activity を終了します。
 * </p>
 */
void popFragment() {
    FragmentManager manager = getFragmentManager();
    if (manager.getBackStackEntryCount() > 0) {
        manager.popBackStack();
    } else {
        getActivity().finish();
    }
}
 
Example 17
Source File: UIService.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Fragment getFragmentByTag(FragmentActivity activity, String tag) {
    if (activity == null) {
        return null;
    }
    FragmentManager supportFragmentManager = activity.getSupportFragmentManager();

    if (supportFragmentManager.getBackStackEntryCount() == 0) {
        return null;
    }
    return supportFragmentManager.findFragmentByTag(tag);
}
 
Example 18
Source File: OdysseyMainActivity.java    From odyssey with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onBackPressed() {

    FragmentManager fragmentManager = getSupportFragmentManager();

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    if (drawer != null && drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else if (mNowPlayingDragStatus == DRAG_STATUS.DRAGGED_UP) {
        NowPlayingView nowPlayingView = findViewById(R.id.now_playing_layout);
        if (nowPlayingView != null) {
            View coordinatorLayout = findViewById(R.id.main_coordinator_layout);
            coordinatorLayout.setVisibility(View.VISIBLE);
            nowPlayingView.minimize();
        }
    } else if (fragmentManager.findFragmentById(R.id.fragment_container) instanceof FilesFragment) {
        // handle back pressed events for the files fragment manually

        FilesFragment fragment = (FilesFragment) fragmentManager.findFragmentById(R.id.fragment_container);

        if (fragment.isRootDirectory()) {
            // current directory is a root directory so handle back press normally
            super.onBackPressed();
        } else {
            if (fragmentManager.getBackStackEntryCount() == 0) {
                // if backstack is empty but root directory not reached create an new fragment with the parent directory
                List<String> storageVolumesList = mFileExplorerHelper.getStorageVolumes(getApplicationContext());

                String parentDirectoryPath = fragment.getCurrentDirectory().getParent();

                onDirectorySelected(parentDirectoryPath, storageVolumesList.contains(parentDirectoryPath), false);
            } else {
                // back stack not empty so handle back press normally
                super.onBackPressed();
            }
        }
    } else {
        super.onBackPressed();

        // enable navigation bar when backstack empty
        if (fragmentManager.getBackStackEntryCount() == 0) {
            mDrawerToggle.setDrawerIndicatorEnabled(true);
        }
    }
}
 
Example 19
Source File: AwesomeFragment.java    From AndroidNavigation with MIT License 4 votes vote down vote up
public int getChildFragmentCountAtBackStack() {
    FragmentManager fragmentManager = getChildFragmentManager();
    return fragmentManager.getBackStackEntryCount();
}
 
Example 20
Source File: DashboardActivity.java    From arcusandroid with Apache License 2.0 4 votes vote down vote up
@Override
public void onBackPressed() {

    if (mNavigationDrawerFragment.isDrawerOpen()){
        mNavigationDrawerFragment.closeDrawer();
        return;
    }
    boolean eventConsumed = false;
    Fragment fragment = BackstackManager.getInstance().getCurrentFragment();

    if (isBaseFragment(fragment)) {
        eventConsumed = ((BaseFragment) fragment).onBackPressed();
    } else if (fragment instanceof NoViewModelFragment) {
        eventConsumed = ((NoViewModelFragment) fragment).onBackPressed();
    }

    if (!eventConsumed) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragment = fragmentManager.findFragmentById(R.id.floating);
        if (isBaseFragment(fragment)) {
            eventConsumed = ((BaseFragment) fragment).onBackPressed();
        }

        if (!eventConsumed) {
            int backStackEntryCount = fragmentManager.getBackStackEntryCount();
            if (backStackEntryCount > 1) {
                BackstackManager.getInstance().navigateBack();
            } else if (backStackEntryCount == 1) {
                fragment = fragmentManager.findFragmentById(R.id.container);
                if (isBaseFragment(fragment) && ((BaseFragment) fragment).isHomeFragment()) {
                    super.onBackPressed();
                } else {
                    BackstackManager.getInstance().navigateBack();
                }
            } else {
                super.onBackPressed();
            }
        }
    } else {
        logger.trace("back press event consumed", "");
    }
}