Navigation Box - Create an instance of a new fragment every time or just “refresh” the data

I have a little argument with myself, and I would like to check with the others which implementation is “best” in terms of best practice.

Like the main Navigation Drawer application, I have one main NavDrawerActivity, which contains the NavDrawer itself, and a frame layout, which I later replaced. Then I also have one fragment called ListsFragment, which basically contains ListView data from a simple String ArrayList. So this Fragment list always contains the main list of strings (which means that I only need one fragment.java file).

Now, in the Google example in the navigation box, they have something similar, where they have one fragment that they just replace with new content (replace, as in the literal sense, FragmentManager.replace (etc.)) . This means that a new fragment is restored every time we select an item from NavDrawer. This is what I am doing right now.

But I was wondering if this would do "dirty." Wouldn't there be a cleaner way to do this:

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 the “cheap” cost of an operation than the need to create an instance of a new fragment every time? When can I just “refresh” data through a single fragment variable that I save?

So my question is: which is better? Or, if "better" is too subjective: moreover, "Java / Android path." It would be great to see what you think. Thanks!

TL; DR: Which is better: creating a new fragment every time I select a new item from NavDrawer or simply load new data using the public loadData method.

EDIT: for those who want to see a Google NavDrawer example, this example can be downloaded here http://developer.android.com/training/implementing-navigation/nav-drawer.html

+4
source share

Source: https://habr.com/ru/post/1497152/


All Articles