Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

// 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?

like image 214
krishan711 Avatar asked Aug 23 '13 08:08

krishan711


People also ask

What are the two ways to add fragments to an activity?

There are two ways to add a fragment to an activity: dynamically using Java and statically using XML.

Is Android fragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle. This class was deprecated in API level 28.

Can fragments be used in multiple activities?

You can use multiple instances of the same fragment class within the same activity, in multiple activities, or even as a child of another fragment.


1 Answers

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.

like image 157
swarajk1 Avatar answered Sep 19 '22 16:09

swarajk1