user940016
user940016

Reputation: 2938

When to use fragments over views?

I read the fragments tutorial, but I still don't understand why they are actually needed. The tutorial gives the example of the 2 fragments in a wide screen and 2 activities in a small one, but I actually could just use a view and put it in the same activity or in another activity to achieve the same effect, so what does a fragment give me that a simple view doesn't? Thanks.

Upvotes: 2

Views: 182

Answers (2)

Muz
Muz

Reputation: 5980

They're suitable when you want to put different content for different types of layouts. Mainly for building an app that's suited to both tablets and phones.

enter image description here

Think of a Fragment like a different activity within the same screen. Sometimes it's easier to have the code itself to be controlled within the fragment, rather than in a master Activity, especially if you intend to split them into separate layouts.

Things like Fragment dialogs are also more powerful than classic Dialogs. Communicating information to a fragment is a little easier and more efficient than between activities (though this may vary according to situation).

If you don't have a reason to use them or don't feel like experimenting, go as simple as possible. There's quite a bit of overhead to Fragments, so unless you're designing for multiple layouts (mainly tablets), it's more work for little gain.

Upvotes: 1

android2013
android2013

Reputation: 425

A fragment has a life cycle of its own , so you don't have to worry about memory and objects in larger screens where this does matter.

Upvotes: 3

Related Questions