Difference between Fragments and Activities

Rahul Dayal Sharma

Ranch Hand

Posts: 34

posted 10 months ago

Hi Joel,

Can you please explain as to when should one use Fragments and when to use Activities ?

Also, I tried creating a multi choice dialog for choosing a phone to connect via bluetooth (which turned out to be a pain on its own), but for some reason the UI look and integration took me quite a while. Does your book provide a walkthrough of the various mobile UI elements that one may require while creating mobile applications? Also, can you suggest any alternative resources for these as the android website although informative, lacks the step by step guidance required for people who are new to android and trying to self learn the fundamentals.

Can you please explain as to when should one use Fragments and when to use Activities ?

Also, I tried creating a multi choice dialog for choosing a phone to connect via bluetooth (which turned out to be a pain on its own), but for some reason the UI look and integration took me quite a while. Does your book provide a walkthrough of the various mobile UI elements that one may require while creating mobile applications? Also, can you suggest any alternative resources for these as the android website although informative, lacks the step by step guidance required for people who are new to android and trying to self learn the fundamentals.

did you know that the fragment always needs an underlying activity?
I would use a fragment when you need o scale the app for a tablet following the famous master detail pattern

said that two activities can communicate really easy between them launching intents, implicit and explicit, for fragment if you do not use a third part library
the communication is completely different and usually follow a programmatic approach defined by a FragmentManager function.

You learn with the experience if you need activities or fragments, there is also an instinctive component, generally speaking activities are simpler because a fragment add a layer, a new level of complexity and a cycle that is different by the one of the underlying activity. Fragment allow you to fill also a small part of the screen, while instead activities take with their extension an entire screen, this is way fragment shine in tablets, where there is more space and you can simply "frame" part of the screen with some specific content, and make different part of the screen communicate using fragments

We coders are all geniuses. Sometimes misunderstood, others humble, introvert or without focus, but how much light abide in our curiosity?(me)

According to Android Developers Docs the following is provided for the relationship between Fragments & Activities:

A Fragment represents a behaviour or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities.

This seems pretty explanatory. As I understand from using fragments the way I do, that they represent, almost the same relationship as includes are to views, i.e. re-useable blocks inserted into other blocks of code. In the case of fragments, they have attached functionality, their own events and inputs, you can add and remove them when the app is running, and as it is mentioned in Android Docs from the link above, they have their own life cycle events. Essentially a modular pattern for Android.

I recommend taking a look at the link above, its a very good explanation, it will help you understand more. I once had the same question! Good Luck!