• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • paul wheaton
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
  • Himai Minh
Bartenders:

Difference between Fragments and Activities

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 600
11
Android Python Open BSD VI Editor Slackware
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Greenhorn
Posts: 11
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic