Saving Android fragments in the navigation box

I have some fragments in my program, and I can replace each fragment by clicking on it using this code. my problem is this: these fragments are low for loading and after clicking on each fragment create a new one from it and cause reloading after clicking on the fragment. how to save fragment contents or state and prevent reload?

@Override
    public void onNavigationDrawerItemSelected(int position) {
        Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = new InfoFragment();
                break;
            case 1:
                fragment = new ParentOtoFragment();
                break;
            case 2:
                fragment = new ParentProFragment();
                break;
            case 3:
                fragment = new SupportFragment();
                break;
            case 4:
                fragment = new AboutFragment();
                break;
        }
        if (fragment != null){
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container,fragment).commit();
        }
    }

AFTER UPDATE

after answering this question @Vikram Ezhil I initialized fragmentsboth fragmentTAGto the constructor and changed the @Vikram Ezhil code to this code below, my problem is solved.

@Override
   public void onNavigationDrawerItemSelected(int position) {
       FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
       fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

       if (getSupportFragmentManager().findFragmentByTag(fragmentTAGS[position]) == null) {
           fragmentTransaction.add(R.id.container, fragments[position], fragmentTAGS[position]);
       }
       for (int i = 0; i < fragments.length; i++) {
           if (i == position) {
               fragmentTransaction.show(fragments[i]);
           } else {
               if (getSupportFragmentManager().findFragmentByTag(fragmentTAGS[position]) != null) {
                   fragmentTransaction.hide(fragments[i]);
               }
           }
       }
       fragmentTransaction.commit();
   }
+4
source share
2

, ,

fragmentTransaction.add(R.id.container,fragment).commit();

fragmentTransaction.replace(R.id.container,fragment).commit();

.

  • , . TAG , TAG . ,

    if (getFragmentManager().findFragmentByTag("fragment1_tag") == null)
    {
        fragmentTransaction.add(R.id.container, fragment1, "fragment1_tag");
    }
    
  • , , , , .

    if (getFragmentManager().findFragmentByTag("fragment1_tag") != null)
    {
        fragmentTransaction.hide(fragment1);
    }
    
  • , . , .

  • , , Android ( ).

- 5 . 5 .

, , , , . ,

InfoFragment fragment1 = new InfoFragment();
ParentOtoFragment fragment2 = new ParentOtoFragment();
ParentProFragment fragment3 = new ParentProFragment();
SupportFragment fragment4 = new SupportFragment();
AboutFragment fragment5 = new AboutFragment();

Fragment[] fragments = new Fragment[]{fragment1,fragment2,fragment3,fragment4,fragment5};
String[] fragmentTAGS = new String[]{"fragment1_tag","fragment2_tag","fragment3_tag","fragment4_tag","fragment5_tag"};

, , , .

@Override
public void onNavigationDrawerItemSelected(int position) 
{
      // Add the fragments only once if array haven't fragment
      if (getFragmentManager().findFragmentByTag(fragmentTAGS[position]) == null)
      {
          fragmentTransaction.add(R.id.container,fragments[position],fragmentTAGS[position]);
      };

      // Hiding & Showing fragments
      for(int catx=0;catx<fragments.length;catx++)
      {
         if(catx == position)
         {
              fragmentTransaction.show(fragments[catx]);
         }
         else
         {
              // Check if the fragment is added and then hide it
              if (getFragmentManager().findFragmentByTag(fragmentTAGS[catx]) != null)
              {
                  fragmentTransaction.hide(fragments[catx]);
              };
         };
      };

      fragmentTransaction.commit();
};
+5

, : , -:

1. :

private Fragment[] fragments = new Fragment[] { new frag1(), new frag2()};

2. onNavigationItemSelected :

        Fragment fragment = null;
        switch (itemId) {
            case R.id.nav_frag1:
                fragment = fragments[0];
                break;
            case R.id.nav_frag2:
                fragment = fragments[1];
                break;
            default:
                return;
        }

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_main, fragment);
        ft.commit();
-1

All Articles