19

Why should Android 3.0 fragments be used instead of compound controls? One can create a View inheritor or compound control once and use it everywhere.

I've read http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html but did not find the answer.

Anton
  • 1,783
  • 3
  • 15
  • 18
  • I've answered this in another question. See http://stackoverflow.com/a/14912608/909956 T;dr - sometimes fragments allow you to create more reusable components than relying on custom view implementation. see the link answer for why. – numan salati Feb 16 '13 at 16:53

2 Answers2

35

The difference is that fragments have a life cycle (onPause, onCreate, onStart...) of their own. By having a life cycle, fragments can respond independently to events, save their state through onSaveInstanceState, and be brought back (i.e. such as when resuming after an incoming call or when the user clicks the back button). The life cycle is summarized in the fragment documentation:

https://developer.android.com/guide/components/fragments.html#Lifecycle

You can always wrap a fragment or activity around a compound view, so just think of fragments as containers to your compound views that give them an independent life cycle.

Lancer521
  • 81
  • 4
  • 11
Henry
  • 2,610
  • 2
  • 17
  • 23
  • 4
    I answered a similar question at http://stackoverflow.com/a/15824301/618881. Someone commented that nested fragments are now allowed in Android 4.2 (API 17). – Henry Jul 24 '13 at 08:38
  • A fragment can contain another fragment. So when you say fragments can't be nested, I'm not sure what you're referring to. Perhaps when you wrote this, that was true? – Stealth Rabbi Jul 01 '14 at 13:36
  • @StealthRabbi, I have removed that caveat since nested fragment support was added in Android 4.2 (API 17). – Henry Oct 07 '14 at 12:17
2

The reason would be to have the same code work on tablets and phones. There are different layout considerations for these devices and Fragments allow you to take that into consideration and have your app behave differently without having to rewrite any code.

CaseyB
  • 24,194
  • 11
  • 70
  • 106
  • 1
    AFAIK, you may use the same compound controls both in android 2.x and 3.x applications. In case of fragments you have to use fragments back compatibility jar. So what makes sense, if there is already an opportunity to use ready UI blocks as compound controls? – Anton Jun 06 '11 at 05:08
  • What I'm saying is that they serve completely different purposes. You cancombine fragments and custom controls but one doesn't take the place of the other. – CaseyB Jun 06 '11 at 12:32
  • Sorry, did not catch that. Why not just create separete layout for each screen size (normal/large/xlarge)? I see no difference, if i use – Anton Sep 08 '11 at 12:16
  • 1
    Because Fragments work more like an Activity instead of just a View and with Fragments you have have multiple on the screen at once that are able to communicate with each other. – CaseyB Sep 26 '11 at 15:02