Wednesday, 14 August 2013

Navigation Drawer - Create New Fragment Instance every time or simply "refresh" data

Navigation Drawer - Create New Fragment Instance every time or simply
"refresh" data

I'm having a bit of an argument with myself and I wanted to check with the
rest of you which implementation is "best" in terms of better practices.
Just like a basic Navigation Drawer app, mine has one main
NavDrawerActivity that contains the NavDrawer itself, and a Frame Layout
that I later replace. Then, I also have one Fragment called ListsFragment,
that basically contains a ListView of data from a simple String ArrayList.
Therefore, this ListsFragment always contains a basic list of Strings
(meaning, I only ever need one fragment.java file).
Now, in Google's example of the Navigation Drawer, they have something
similar, where they have one Fragment that they simply replace with new
content (replace as in, literally, FragmentManager.replace(etc)). This
means that a new Fragment is reinstantiated each time we select an item
from the NavDrawer. This is what I'm doing right now as well.
But I've been thinking whether or not doing this is "messy". Won't a
cleaner way of doing this be:
Create a method in the Fragment called loadList(ArrayList<String> list),
which basically replaces the contents of the ListView in the Fragment with
the consumed list.
After all, isn't this a "cheaper" operation cost than having to
instantiate a new Fragment everytime? When I could just be "refreshing"
the data through a single Fragment variable I'll maintain?
So, my question is: Which is better? Or if "better" is too subjective:
Which is more, "the Java/Android way". It'd be great to see what you
think. Thanks!
TLDR: Which is better: Instantiating a new Fragment every time I select a
new item from the NavDrawer or simply load new data through a public
loadData method.

No comments:

Post a Comment