Guilherme Moura
Guilherme Moura

Reputation: 33

Do fragments mean I should code single-activity apps?

OK, I get the concept of Fragments being modular/ interchangeable "sub-activities" but given that fragments have their own lifecycle much like the one of an activity and can pretty much do anything that an activity can, from a best-practice standpoint, does that mean we are to start coding a single activity for the whole app (say, the main_activity) and simply adding/ replacing/ removing fragments within that single activity?

If not, then how do I know when it's time to create a new activity instead of continuing to push new fragments into the same old activity?

Just trying to understand how to best organize my app's features into activities and frags. A practical example would help (no code necessary, just the concepts).

Cheers,

Upvotes: 3

Views: 247

Answers (2)

NeuraCache
NeuraCache

Reputation: 14174

No, I does not mean that you should use single activity.

Fragments are to help you organize ui elements (especially on big screens (like tablets)). They also introduce new layer of reusability (using fragments for loaders, simple views) across your projects.

I recomend you check Google NewsReader SDK example which is a great way to see how to implement activity/fragment patterns depending on what type of screen it is launched on

http://developer.android.com/training/multiscreen/adaptui.html

Download sample button is on the right

Upvotes: 1

Trinimon
Trinimon

Reputation: 13957

No fragments are just one way to reuse parts of an application. For instance you can use a fragment in several activities. On the other hand you can of course have several activities that use different fragments or do not use fragments at all.

So summary: no you don't need to. Fragments are helpful, if you build up multiple activities that use a similar or equal component as a part of their layout.

Upvotes: 1

Related Questions