Intro:
The basic "Fragments Tutorial" pattern goes something like this:
- On a tablet, have a list on the left, details on the right.
- Both are
Fragments
and both reside in the sameActivity
. - On a phone, have a
list
Fragment
in oneActivity
. - Launch a new
Activity
with the detailsFragment
.
(e.g. Android 3.0 Fragments API by Dianne Hackborn and the Fragments API Guide)
On both devices, functionality is in the Fragments
. (simple)
On the Tablet, the whole app is 1 Activity
, on the phone, there are many Activities
.
Questions:
- Is there a reason to split the phone app into many
Activities
?
One problem with this method, is that you duplicate a lot of the logic in the main Tablet Activity
, and in the separate Phone Activities
.
- Would it not be easier to retain the 1 Activity model in both cases,
using the same logic of switching
Fragments
in and out (just using a different layout)?
This way most of the logic resides in the Fragments
themselves, and there is only a single Activity
- less duplication of code.
Also what I have read about the ActionBarSherlock
is that it seems to work best with Fragments
instead of Activities
(but I have not worked with it yet).
Are the tutorials oversimplified, or have I missed something major in this approach?
We have tried both approaches successfully in the office - but I am about to start a bigger project and want to make things as easy for myself as possible.
Some links to related questions:
- Dilemma: when to use Fragments vs Activities:
- Patterns when to use Activity Transition vs Dynamic Fragments
- Android - I need some clarifications of fragments vs activities and views
- Activities or fragments in Android?
- Multiple fragments and activities interaction design
- So what are the exact advantages of Fragments in Android 3.0?
Updates
Started bounty on question - still not convinced about why I need to duplicate my app logic in my tablet activity and in each phone activity.
Also found an interesting article by the guys at Square, which is well worth reading:
onItemSelected()
method in the Activity. In my "real" app, I have many lists & sublists. This pattern suggests that my Tab Activity must have anonItemSelected()
method to handle each of the lists. Plus the Phone Activities must each have that same logic duplicated inside each of them. IMHO it is much better to put the Item Selected logic into each Fragment - there is no duplication and I prefer that way of structuring the code. I hope this helps – Richard Le Mesurier