What's the difference between changing two fragments with FragmentManager and two views with ViewSwitcher?

518 Views Asked by At

When to choose between using two fragments and replacing one with the other in an activity (or child fragments in another fragment) or using two views and changing them using ViewSwitcher (or ViewFlipper)?

For example, while loading the data, I use a loading view, and then I want to change to the main view.

2

There are 2 best solutions below

1
On

It really depends on your app structure. A ViewSwitcher may work, but is likely not the best way to do this. Another option you could do is not do a fragment at all, just change the content of the activity, but calling again setContentView(int) with the layout resource of the main view, once the loading is complete.

0
On

I never have used ViewSwitcher before. I read the documentation on the class. And I think ViewSwitcher is good for switching between 2 different views or layouts, only 2. More than 2, and you have to create another ViewSwitcher. The benefits that I see of ViewSwitcher are:

  1. All the views (only 2 at most) and its objects are kept in memory. So it's fast.
  2. It is meant to be fast since it extends ViewAnimator, made for animation.

Now for Fragments...It is commonly used by developers like myself. It has good support for managing many (not just 2) layouts. With help of FragmentManager, you can add, replace, or search for fragments. The benefits of Fragments are:

  1. Management of UI is robust.
  2. Android framework and system will manage memory to allow other apps to run. As a warning, this may be an issue with using Fragments and handling objects in memory.
  3. It is memory efficient and I think it's quite fast. Coding fragments may be more of an issue. However since soo many developers use it, you have much support, like from me, in the SO community and Internet.

Good luck and have fun with Android :-)