• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Newbie question - Use Activity or Fragment

Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello to the forum. I am learning a little about Android programming and have a couple of questions. I've just gone through information on some of the basics and I think I get the overall picture but one thing that is confusing me a little bit is the difference between an Activity and a Fragment and when to use each?

So I went through and did a small sample that shows how to create an Activity and then call and pass information to another activity. I've read about but have not tried to use Fragments yet but the way I see the difference is...and please correct me if I am wrong...

An Activity is basically a screen of your app. If your app has multiple different screens like say a login screen, an entry screen, a screen that shows before you go then you can have activities for each of them

A fragment is a part of a screen, or it can be the whole screen depending. It has to be associated with an activity even if it will cover the whole screen. It really comes in handy when you have larger screens like on a tablet where you can have two fragments on the screen at the same time, each doing different stuff.
I read a tutorial which shows how to replace one fragment with another within an activity but have not had a chance to try it.
this is what I plan on doing to help myself learn Android. I was going to build a basic quiz app. 3 questions. App begins on "question page 1", user selects an answer and it goes to an "answer page 1" which tells the user if they were right/wrong and the score is added if they are correct, then it goes on to "question page 2" and so on until all 3 question pages and answer pages have been went through by the user.

Now what I was wondering, is that something I should do with activities or with fragments and just keep replacing fragments. like have 3 question fragments and 3 answer fragments.
Also if I should use fragments would it be better to just use 1 question fragment and 1 answer fragment and just load the questions from a resource file or is that too complex for a beginner?
Like say for question 1, the lone "question fragment" would load the "question1 info" which includes the questions, choices, and answer for question 1, then for the next iteration, this same fragment would load the "question2" info.

If anything in my description didn't make sense please let me know. I am just learning Android so even some of the concepts I "think" I know might be wrong so let me know if something I said above does not make sense.

Posts: 598
Oracle Notepad Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An activity is the code in control. It usually has a screen, but it does not require one.

A fragment is a form or part of a form that can be used inside another object or form. A simple example is a custom listbox that shows a particular set of controls for each item listed. To do that, the controls are put inside a fragment, and the listbox is set to repeat that fragment.
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
a bit of art, as a gift, the permaculture playing cards
    Bookmark Topic Watch Topic
  • New Topic