Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between Fragments and Activities

 
Rahul Dayal Sharma
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Giovanni Montano
Ranch Hand
Posts: 250
4
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Dayal Sharma wrote: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.


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


 
Stephen M Davies
Greenhorn
Posts: 11
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic