Android: Is it better to create a new fragment every time a navigation drawer item is clicked, or load up previously created fragments?

I am implementing the standard navigation drawer pattern for android, with about 10 fragments the user can navigate to from the drawer. Currently, I am creating a new Fragment every time a different navigation drawer item is clicked like so:

// When a new navigation item at index is clicked 
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Fragment newFragment = null;
if (index == 0)
    fragment = new Fragment0();
...
ft.replace(R.id.container, newFragment);
ft.commit();

I have been wondering if it would be more efficient to do something like the following:

  • Android emulator shows nothing but blank screen
  • Gson - convert from Json to a typed ArrayList<T>
  • Android games development SDK?
  • Determining the current foreground application from a background task or service
  • ActionBarActivity with ActionBarDrawerToggle - Not using drawerImageRes
  • How to give percentage values in ObjectAnimator in Android
  • // Somewhere in onCreate
    Fragment[] fragments = new Fragment[n];
    fragments[0] = new Fragment0();
    fragments[1] = new Fragment1();
    ...
    
    // When a new navigation item (at index) is clicked 
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.replace(R.id.container, fragments[index]);
    ft.commit();
    

    My main worry is that some of the fragments hold a significant amount of data (fairly large lists and lots of views). Would there be any issue holding all these fragments in memory and would it provide any advantage over instantiating new fragments every time (apart from faster switches between fragments)? Is there a generally accepted ‘better’ solution?

  • Add Google Play Services to Android Studio using Gradle
  • How to determine application's method count?
  • java library vs android library
  • Android: Insert text into EditText at current position
  • Android Fatal Signal 11
  • Change Spinner DropDown width
  • One Solution collect form web for “Android: Is it better to create a new fragment every time a navigation drawer item is clicked, or load up previously created fragments?”

    I had a similar issue. I took an approach similar to yours, and to save load on processor I made the following tweak to every call to create an instance for a Fragment object:
    (in context to my use in selectItem method)

    switch (position) {
        case 0:
            if (fragmentOne == null)
                fragmentOne = new FragmentOne();
            getSupportFragmentManager().beginTransaction().......
            break;
        case 1:
            if (fragmentTwo == null)
                fragmentTwo = new FragmentTwo();
            getSupportFragmentManager()......
    

    FragmentOne and FragmentTwo are the two different Fragment classes and fragmentOne and fragmentTwo are their objects declared as fields in MainActivity

    Edit:

    I would like to add how you could follow a similar approach with the array holding the fragments. Instead of creating new fragments in onCreate, do that when an item is clicked. In onCreate, send a call to the drawer to select the fragment which you want selected by default on launch.

    //somewhere in onCreate
    if (savedInstanceState == null) {
        selectItem(defaultFragment);
    }
    else
        selectItem(selectedFragment);
    //selectItem is method called by DrawerItemClickListener as well
    
    
    //in selectItem: when navigation item at index is clicked, the listener calls this
    switch (index) {
        case 0:
            if (fragments == null) {
                fragments = new Fragment[n];
                fragment[0] = new Fragment0();
            }
            else if (fragments[0] == null) {
                fragments[0] = new Fragment0();
            }
            break;
        ....
    }
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ...
    ft.replace(R.id.container, fragments[index]);
    ft.commit();
    

    No need to instantiate the fragments on onCreate since that makes the startup slow. Create them as and when needed and afterwards use the same one if it exists. The new fragments are created only if they are being loaded for the first time.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.