2

Just a disclaimer, I am pretty new to Android and slowly working through tutorials. Most tutorials dont talk about fragments at all in the beginning, but Android-studio by default sets up one for you.

I've read some of the past questions and the dev blog related to fragments and activities and they were helpful in giving me an idea as to the advantages of using fragments.

I am still a bit confused on when one would use a new activity in an app, it seems to me like everything could be accomplished with fragments and a single activity.

Lets say an app has multiple screens, do you implement that as one activity with multiple fragments, or multiple activities with one fragment each to them.

image

This image makes sense to me and demonstrates the power of fragments, but why on the handsets example is two activities required?

Another add-on question, should everything moving forward be done in a fragment?

Thank you and sorry if these questions didnt really make sense.

Community
  • 1
  • 1
Lynx
  • 57
  • 5
  • 1
    http://stackoverflow.com/questions/20306091/dilemma-when-to-use-fragments-vs-activities or http://stackoverflow.com/questions/18885311/use-activity-or-fragment-in-android-application – Mdlc Feb 13 '14 at 18:39

3 Answers3

3

An Activity should be the host for a collection of related Fragments. For instance, you might have something like:

Base Activity extends FragmentActivity

LoginActivity extends BaseActivity
    -- LoginFragment
    -- LoginErrorFragment
    -- LoginSignUpFragment

SettingsActivity extends BaseActivity
    -- SettingsGeneralFragment
    -- SettingsAdvancedFragment

If you try to move all of your logic into a single Activity, it's going to get unmaintainable very quickly. Another good practice is to have a base Activity which all of your Activities extend; since if you suddenly find that there's some functionality you want to provide to all activities, you can just add it to the base class.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • Thanks, this along with the link from the other poster (which I some how missed in my search) helps clarify things a bit. – Lynx Feb 13 '14 at 18:52
  • Sorry just to clarify something you said, you are saying its good practice to have a 'main activity' which supercedes all of the activities in an app? So in the example you gave you'd have 'MainActivity' above LoginActivity and SettingsActivity. – Lynx Feb 13 '14 at 18:55
  • 1
    Correct. We have a `BaseActivity` class that all of our other activities extend. – Kevin Coppock Feb 13 '14 at 18:56
3

Everything said above in both regards are absolutely correct. I would just like to add few points to them.

  1. When thinking about fragments please keep in mind that they are a part of an Activity, which like any other view can be added, modified and replaced dynamically. For example, while using ActionBar's and Navigation Drawers fragments become more handy and flexible. Similar things stands true for ViewPager etc.
  2. Fragments also cater to larger screen sizes in a much better way than the traditional Activity approach. Imagine the users experience then when for every action performed a new screen would replace the Phone/ Tablet against now when all the actions and their performed events lie on the same screen.

One more thing which I like about fragments is, we dont have to declare them in the Manifest. :) Most of the time we forget to do that with Activities until the compiler prompts. :) (At least me)

Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
0

As you said. In simple application you can use only one Activity and just replace fragments. I did it in my apps and it works perfect. Sometimes you just need to start new Activity if you want to follow android design and architecture patterns.

  1. According to your question about images that you posted you can obtain the same effects using just one Activity and Fragments.
  2. Yes everything moving forward can be done in a Fragment.
Piotr Ślesarew
  • 2,826
  • 1
  • 17
  • 17