0

I had created a frame layout at my Main Activity and added fragment 1 into the layout when the app launch.

Fragment fr;
fr = new fragment1();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit(); 

After this when the user presses something on Fragment 1, it will start fragment 2.

Fragment2 newFragment = new Fragment2();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_place, newFragment);
transaction.addToBackStack(null);
transaction.commit();

The problem now is when I press back button on fragment 2, it will back to fragment 1 but reload fragment 1. And when I press back button again, it will back to fragment 2 again. Anyone can help me on this question as I only want fragment 1 to be saved at all the time and fragment 2 is destroy when the user presses the back button.

Evin1_
  • 12,292
  • 9
  • 45
  • 47
Ryan Lim
  • 206
  • 5
  • 14

3 Answers3

0

And when i press back button again, it will back to fragment 2 again.

This happen because when you load fragment 2, you added it into backstack, so remove this line of code

Fragment2 newFragment = new Fragment2();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_place, newFragment);
//transaction.addToBackStack(null); <-- remove this
transaction.commit();

The problem now is when i press back button on fragment 2, it will back to fragment 1 but reload fragment 1.

If you return to a fragment from the back stack it does not re-create the fragment but re-uses the same instance and starts with onCreateView() in the fragment lifecycle, see Fragment Lifecycle. This answer from here

Community
  • 1
  • 1
Fuyuba
  • 191
  • 1
  • 8
0

When you add a fragment to the activity for the first time you need write fragmentTransaction.add(R.id.fragment_place, fr) instead of fragmentTransaction.replace(R.id.fragment_place, fr);

You are initializing fragment 1 from MainActivity for the first time so your code looks something like this

Fragment fr;
fr = new fragment1();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.fragment_place, fr);
fragmentTransaction.commit(); 

You see that I also remove fragmentTransaction.addToBackStack(null); from my code.

Because

You said that when you go to fragment 1 and press back button it return back to fragment 2. That's because you add fragmentTransaction.addToBackStack(null); to the first fragment fragment. So when you come back to fragment 1, android add fragment 2 in back stack of fragment 1. So when you press back button it returns back to fragment 2.

And when you going to another fragment from a fragment then you need to use fragmentTransaction.replace();

Your final code look like this

Fragment fr;
fr = new fragment1();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.fragment_place, fr);
fragmentTransaction.commit();

And

Fragment2 newFragment = new Fragment2();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_place, newFragment);
transaction.addToBackStack(null);
transaction.commit();

Hope this helps!

Niyamat Ullah
  • 2,384
  • 1
  • 16
  • 26
0

I find an easy way. kotlin code:

    var fragmentManager = supportFragmentManager
    if (getSupportFragmentManager().getBackStackEntryCount() > 0)
         fragmentManager
        .beginTransaction()
        .replace(R.id.frg_holder, yourNewFragment)
        .commit()
    else
        fragmentManager
        .beginTransaction()
        .replace(R.id.frg_holder, yourNewFragment)
        .addToBackStack("tag")
        .commit()
mhKarami
  • 844
  • 11
  • 16