1

I'm trying to show two Fragments in a FrameLayout container. I only show one at a time and hide another one based on my application logic. I believe every time I change my device orientation, they recreate! I can see duplicates of fragments overlapping each other! How to fix it? Thanks in advance.

public class MainActivity extends AppCompatActivity {

   final Fragment oneFragment = oneFragment.newInstance();
   final Fragment twoFragment = twoFragment.newInstance();
   Fragment active = oneFragment;

   final FragmentManager fm = getSupportFragmentManager();

@Override
   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       // some other code

        fm.beginTransaction().add(R.id.container, oneFragment, "1").commit();
        fm.beginTransaction().add(R.id.container, twoFragment, "2").commit();

        // onclick listener 
        if(condition) {
            fm.beginTransaction().hide(active)
            .show(oneFragment).commit();
        } else {    
            fm.beginTransaction().hide(active)
            .show(twoFragment).commit();
        }

      // some other code

2 Answers2

2

Fragments are always restored from a previous state automatically after Activity restart.

To avoid this behavior just check if savedInstanceState is null or not. If it is null - we have a new activity instance, if not - activity was recreated (so, no need to add fragments again - they are already there).

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   if (savedInstanceState == null) {
      fm.beginTransaction().add(R.id.container, oneFragment, "1").commit();
      fm.beginTransaction().add(R.id.container, twoFragment, "2").commit();
   }
} 

Take a look at this article (section Add a Fragment to an Activity at Runtime) for additional info, it will help you with fragment magic :) https://developer.android.com/training/basics/fragments/fragment-ui.html

Alexander Bilchuk
  • 1,790
  • 1
  • 12
  • 17
  • this stops recreating fragments but switching to other one is not working anymore! – user2876313 Jul 05 '17 at 15:07
  • Of course, you should fix you switching logic using information I gave you (I don't see you switching logic, that your condition means, your click listeners, etc). Also I recommend you to read the article above: you can find there examples how to switch fragments, how to work with Fragment Manager, etc. Take you time, read the documentation carefully and everything will become clear ;) – Alexander Bilchuk Jul 05 '17 at 15:13
  • Thank you. I have updated your solution. It works for me now. – user2876313 Jul 05 '17 at 16:19
0

try this:

 if (condition) {
    replaceFragment(oneFragment, "1")
 } else {
    replaceFragment(twoFragment, "2")
 }

 private void replaceFragment(Fragment fragment, String tag) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    transaction.addToBackStack(tag);
    transaction.replace(R.id.container, fragment, tag);
    transaction.commit();
}
  • how to do that if 3 fragments there instead of 2? A<>B<>C – user2876313 Jul 05 '17 at 15:48
  • Please add more informations or valuable link to understand your answer – Yagami Light Jul 05 '17 at 16:36
  • onPause, onResume, onCreateView and other life cycle events will be invoked in case of replace but they wont be invoked in case of add. So if you you register/unregister some events in onPause/onResume be carefull. [https://stackoverflow.com/questions/18634207/difference-between-add-replace-and-addtobackstack] @user2876313 using replace you may replace any containers with any fragments you want. – Vitaliy Burov Jul 05 '17 at 20:29
  • **Replace** removes existing fragment **Add** retains one. After both methods add new fragment. – Vitaliy Burov Jul 05 '17 at 21:15