0

I have 3 fragments: Home, A and B. Home and A are in mobile navigation menu.

User goes from A to B and then press back button. The issue that if I use getFragmentManager().popBackStack(); onCreateView of fragment A is calling and I have duplicating of content.

But if I use getActivity().onBackPressed(); it goes to Home fragment instead of A.

How to display fragment A by clicking back button without refreshing the view?

Here is how I make transaction from A to B

 CertificateItemFragment certificateItemFragment = new CertificateItemFragment(item);
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.nav_host_fragment, certificateItemFragment).addToBackStack(null);
    fragmentTransaction.commit();

2 Answers2

3

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

Hababa
  • 551
  • 5
  • 8
1

In normal case navigation item list menu open fragment which are replace to each other but when we move inside of any item as like in your case move from fragment A to B, In this case normally we use another activity on which create fragment.

If you do't want to use activity then, you just need to add fragment.

try below code that may help you

CertificateItemFragment certificateItemFragment = new CertificateItemFragment(item);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.nav_host_fragment, certificateItemFragment).addToBackStack(CertificateItemFragment.TAG);
fragmentTransaction.commit();
webaddicted
  • 1,071
  • 10
  • 23