I am new to android and I am trying to understand how to make re-usable UI pieces that I can interact with. I know there are a few techniques out there but I don't understand when one would be better than the other or if they can all be used interchangeably. If someone could point out the differences (or if I've misunderstood), that would be appreciated.
- Creating a fragment or fragmentActivity that takes care of the new page. This fragment can then be used whenever applicable but special care must be taken to take care of fragment life cycles.
- Extending a base class that has the layout and whatever else is necessary. Any other class can then extend this base and have the base UI also appear and work.
- Using
<include layout=...
lets you re-use only pieces of the UI. For this one, I am not sure where the logic for that layout is supposed to go.
I guess what I'm asking for is a run down of each of these and if I can use whichever one I am more comfortable with without having any negative impacts.
Ps: I'd be using these for things like creating a reusable navigation bar I can put on every page or if different pages all have a help button.
Each of the methods you suggest are indispensable for creating reusable user interfaces, and each have different uses.
Firstly, layouts just define the visual structure of a user interface, while activities and fragments are the components that define how that visual structure acts. This creates a clear separation between the visual structure and the behavior of a user interface.
Therefore, the
<include/>
tag is used to reuse a defined visual structure. This visual structure can be included in any layout, but each activity or fragment can define how that visual structure behaves separately.In order to create a reusable behavior, you can subclass an
Activity
. For example; aFragmentActivity
retains the behaviors defined in theActivity
class, but adds the ability to house one or more fragments. Any class that subclassesFragmentActivity
will keep each of these behaviors as well.Fragments were developed to be reusable segments of an
Activity
, and can define their own visual structure and behavior. Therefore, if you want to be able to reuse both the visual structure and behavior of a section of anActivity
, you can create aFragment
in order to do so. Fragments can be subclassed as well, this can be used to modify the visual structure, behavior or both of the extended class.And these are not the only ways to create a reusable interface. You can define reusable resources, views, styles or animations to create a modular user interface. Learning how to leverage these various components to suit your needs is a big part of Android development (and development in general).
Lastly, I will address your post script; how to create a reusable navigation bar for each page, which includes a help button. Hopefully this will show a simple way to create a reusable interface, and that it is not such a daunting task.
One way of implementing a reusable help button for each
Activity
is to create amenu
resource that can be used as an options menu in a base class:Then this menu resource is inflated within the base class. The base class extends
AppCompatActivity
(which extendsFragmentActivity
) in order to make use of theSupport Library
:With this you can either handle the help button click in the base class, or have a child class override
dispatchHelpIntent()
to handle it separately. You can also overrideonCreateOptionsMenu()
andonOptionsItemSelected()
in a child class to add more items to the menu. Just make sure you callsuper
in both methods, as I do here, in order to retain the help button.Next, to create a reusable navigation bar (usually called an App Bar in Android), all you have to do is create a
layout
file that will be included in thelayout
file for eachActivity
:Then you can include this layout in any other
Activity
by using the<include/>
tag:And finally add this as the
ActionBar
("Action Bar" is the old term for App Bar) in anyActivity
that extends theBaseActivity
:The reason this cannot be done in the
BaseActivity
is because, unless everyActivity
uses the same layout, eachActivity
must define its own layout withsetContentView()
. TheToolbar
will not be available until the content view is set, so you must set up the Action Bar (App Bar) in each activity separately with this method.