Thursday, 5 September 2013

Android Fragments: Sharing Code with Dual UIs (stack-of-cards phone and side-by-side tablet)

Android Fragments: Sharing Code with Dual UIs (stack-of-cards phone and
side-by-side tablet)

I have a typical dual UI scenario - a list of categories, which when one
is clicked, loads a category detail fragment containing a list of items in
that category. On the phone, it's implemented as a stack-of-cards UI,
opening up the details in a separate activity on top of the category list.
On a tablet, it's the category list on the left, with the details on the
right.
In the details pane, there's a button to add an item. The details fragment
has an interface, required of Activities, with an onClickAddItem method,
which should bring up a DialogFragment to ask you for the details of the
item and add it when it returns.
The problem: both the tablet version's all-in-one Activity and the phone's
standalone details Activity need the same onClickAddItem logic. There's a
sinking feeling deep in my gut that the proper solution for this is to
pull that logic out into yet another class, but the need to create several
million files to do simple things in Android is slowly driving me insane,
so I'm hoping there's another best practice I'm overlooking here. Thanks!

No comments:

Post a Comment