Programmatically Go Back to the Previous Fragment in the Backstack

Programmatically go back to the previous fragment in the backstack

Look at the getFragmentManager().popBackStack() methods (there are several to choose from)

http://developer.android.com/reference/android/app/FragmentManager.html#popBackStack()

Programmatically go back to the previous fragment

I achieved it by doing following:-
1. Do no add fragment transaction in back stack.
2. On back press check if current displaying fragment is not home fragment then show home fragment other wise super.onBackPressd() to close activity.

 if (mDrawerLayout.isDrawerOpen(GravityCompat.START))
{
mDrawerLayout.closeDrawer(GravityCompat.START);
}
else if (fragmentManager.findFragmentById(R.id.content_frame_drawer_layout) != homeFragment)
{
fragmentManager.beginTransaction().replace(R.id.content_frame_drawer_layout, homeFragment).commit();

}
else
{
super.onBackPressed();
}

How do I go back to previous fragment programatically with navigation component in kotlin?

You can just call popBackStack() method on your NavController, on click of that button
Something like:

findNavController().popBackStack()

For more check Navigation Component docs

How to go back to a previous fragment on click of phone's back button?

Your all fragment commit with add() method instead of replace() method. Something like this

android.app.Fragment fragment = new YourFragment();
FragmentManager frgManager = getFragmentManager();
android.app.FragmentTransaction ft = frgManager.beginTransaction();
ft.addToBackStack(null);
ft.add(R.id.content_frame, fragment);
ft.commit();

How do you return to the previous fragment when using a NavController?

If all that you want to do is pop the back stack, setGraph() is unnecessary. Just call popBackStack() on your NavController.

How to return to Previous Fragment by clicking back(Not Hardware back button)?

You can do it by using the popBackStack() method of FragmentManager, put this inside the onClickListener of your back button :

if (getFragmentManager().getBackStackEntryCount() != 0) {
getFragmentManager().popBackStack();
}

If you're using the default back button of the toolbar i.e, the home button, then you can do it by placing this code in the onOptionsItemSelected() method of your Activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
if (getFragmentManager().getBackStackEntryCount() != 0) {
getFragmentManager().popBackStack();
}
return true;
}

return super.onOptionsItemSelected(item);
}

or, if you want the same behaviour on hardware back button press then override the onBackPressed() method in your Activity class like this:

@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() != 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}

Android Back to previous fragment by popBackStack

You need to know some premise before using FragmentManager.

  1. in Activity there is FragmentManager and we should call it with getSupportFragmentManager(), getFragmentManager() is deprecated.

  2. in Fragment there are multiple FragmentManagers called ParentFragmentManager and ChildFragmentManager, the final FragmentManager which is deprecated too. And ParentFragmentManager will be same as Activity's FragmentManager

  3. getActivity().onBackPressed() will pull out fragment if any stack exists in Activity's FragmentManager

  4. fragmentManager.popBackStack() will pull out fragment if any stack exists in Activity's or Fragment's FragmentManager rely on who calls


Base on above points

(1) If You want to hold fragments in Activity, you should call getSupportFragmentManager() in Activity, and call getParentManager() in Fragment, then onBackPressed() will pull out the fragment you last add to stack.

(2) If you want to hold fragments in a Fragment and Separated from Activity, you should call getChildFragmentManager() in Fragment, then activity.onBackPressed() will pull out the fragment which in Activity's fragment stack and ignore other fragments in Fragment's fragment stack.


For question will be same as case (1), and if you dont want re-create fragment, you should use add() instead of replace()

in Fragment A

CertificateItemFragment certificateItemFragment = new CertificateItemFragment(item);
FragmentManager fragmentManager = getParentFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.nav_host_fragment, certificateItemFragment).addToBackStack(CertificateItemFragment.TAG);
fragmentTransaction.commit();

now onBackPressed() will backstack from CertificateItemFragment to Pre-Fragment



Related Topics



Leave a reply



Submit