Honeycomb SDK example

As i said in my last post – i did not have a look into the actual API or any honeycomb based code yet. But today i created the Gallery sample application that comes with the preview SDK. And i actually found the sample application quite interesting!

So let’s start with this funny blog post, i will update it while i discover both the source as well as the application inside the emulator. I just write down what i find interesting and let you know!

The first thing that immediately caught my attention was a Day and Night mode. I did not hear of that before and therefore i thought i’ll put it on here:

Night mode of the Honeycomb sample SDK

Day mode of the Honeycomb sample SDK

It definitely seems like the sample application is the best place to start learning more about the new ActionBar, the Fragments system and how to structure your future Android projects (if they all will be based on fragments, which is my best guess).

Aaaaand i just found the first example for the new animation framework. If you click on the ActionBar on “Hide Titles” an ObjectAnimator will be used to fade away the list (the ListFragment on the left).

Just looking at the MainActivity.java you can find out quite some stuff. For example look at the management of Fragments:

final FragmentManager fm = getFragmentManager();
final TitlesFragment f = (TitlesFragment) fm
                           .findFragmentById(R.id.frag_title);
final View titlesView = f.getView();
...
 if (shouldShow) {
          fm.beginTransaction().show(f).commit();
...
 fm.beginTransaction().hide(f).commit();

It looks like they implemented some kind of Transaction management for showing and hiding fragments. What you can also see from this code is that a Fragment is not a View. It’s some kind of wrapping element around views.

It even looks like you can search for fragments that were on the previous activity stack and not in the current, see this documentation of the function findFragmentById (int) inside the FragmentManager:

Finds a fragment that was identified by the given id either when
inflated  from XML or as the container ID when added in a
transaction.  This first  searches through fragments that are
currently added to the manager's  activity; if no such fragment is
found, then all fragments currently  on the back stack associated
with this ID are searched.

I just discovered yet another feature of the app – or an animation – or both, whatever you call it. If you click (well – tap) on the right fragment (the view with the image) the ActionBar actually hides after a smooth animation. This way you get even more space to use for your application content. I wonder if this will be the case with all honeycomb apps, or if it is hidden somewhere in the example code. See the following picture that shows the UI without the ActionBar.

Example app without the ActionBar

I just decided to have a closer look at the developer documentation that comes with the SDK. As it is not yet available online i thought i post some interesting snippet about Fragments here:

A Fragment is a piece of an application's user interface or behavior
that can be placed in an Activity. Interaction with fragments is
done through FragmentManager, which can be obtained via
Activity.getFragmentManager() and Fragment.getFragmentManager().

The Fragment class can be used many ways to achieve a wide variety
of results. It is core, it represents a particular operation or
interface that is running within a larger Activity. A Fragment is
closely tied to the Activity it is in, and can not be used apart
from one. Though Fragment defines its own lifecycle, that lifecycle
is dependent on its activity: if the activity is stopped, no
fragments inside of it can be started; when the activity is
destroyed, all fragments will be destroyed.

All subclasses of Fragment must include a public empty constructor.
The framework will often re-instantiate a fragment class when
needed, in particular during state restore, and needs to be able to
find this constructor to instantiate it. If the empty constructor
is not available, a runtime exception will occur in some cases
during state restore.

And even more, the documentation covers the Back Stack and Layouting for Fragments.

The Back Stack directly relates to the FragmentManager and the transaction based view stack of fragments (remember show().commit and hide().commit()?). Fragments can be pushed onto this stack and are removed if the user triggers the “back” button. This is a totally different approach as the current system. Right now back moves through activities. With Honeycomb you will be able to move through several parts of an activity by using the Fragment system.

Fragments layouting. Yeah this one is a definite must read! With honeycomb you can now use a new tag inside the activity layout XML. See this example from the documentation:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
            android:id="@+id/titles"
            android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>

Besides the standard fragment class there is also a ListFragment which is used inside the sample application. The API documentation also covers some more code about the ListFragment, see the following ListFragment example:

public static class TitlesFragment extends ListFragment {
    boolean mDualPane;
    int mCurCheckPosition = 0;
    int mShownCheckPosition = -1;

   ...

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        showDetails(position);
    }

    /**
     * Helper function to show the details of a selected item, either by
     * displaying a fragment in-place in the current UI, or starting a
     * whole new activity in which it is displayed.
     */
    void showDetails(int index) {
        mCurCheckPosition = index;

        if (mDualPane) {
            // We can display everything in-place with fragments, so update
            // the list to highlight the selected item and show the data.
            getListView().setItemChecked(index, true);

            if (mShownCheckPosition != mCurCheckPosition) {
                // If we are not currently showing a fragment for the new
                // position, we need to create and install a new one.
                DetailsFragment df = DetailsFragment.newInstance(index);

                // Execute a transaction, replacing any existing fragment
                // with this one inside the frame.
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.details, df);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
                mShownCheckPosition = index;
            }

        } else {
            // Otherwise we need to launch a new activity to display
            // the dialog fragment with selected text.
            Intent intent = new Intent();
            intent.setClass(getActivity(), DetailsActivity.class);
            intent.putExtra("index", index);
            startActivity(intent);
        }
    }
}

The most interesting piece here is something that i hoped we – as developers – can avoid. You need to decide for yourself if you have multiple fragments and want to display a View on a fragment, or if you want to start this in a separate Activity. This is, what i believe, the system to handle both phones and tablets in the same code. I hoped for a better solution to handle this, for something that just works automatically.
From the looks of that code you need to handle dual panes on your own. Let’s see if the team at Google comes up with an even simplier solution to that.

edit:

some more stuff i just discovered:

There is a FragmentBreadCrumbs class which can be used as a view inside the ActionBar using the method ActionBar.setCustomView (). This i really cool if you have an application that uses the fragment system heavily. Think of an file browser where you open folders in fragments recursively – you get the breadcrumb navigation for free! … Can’t wait to see that in action in other applications.

And yet another thing that is actually also inside the sample application. Tabs on the ActionBar! Yes, they just work like traditional TabHosts, with the one difference that you can combine them onto the ActionBar together with other actions and menus. I like this idea.

Alright. That’s it for today. I will finish this post now and start trying to write some of my own fragments. Maybe i write something again when i got my first own hands on impressions on how honeycomb code works.

Advertisements

About atla

codemonkey
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

3 Responses to Honeycomb SDK example

  1. Frank Denton says:

    I think you are on to something. There are a lot of us programmers out here that really just want to beat that initial hurdle of being able to comprehend one good application. The Honeycomb Gallery is not just the best application to dissect and learn, but really, the only one. The problem is that there simply isn’t any ‘what did this line do?’ description on which to build upon.

    I have no doubt that a lot of Honeycomb applications will have an interface similiar to the Gallery; only because it is the only honeycomb program that works for the Xoom. I know Tablets are here to stay, and the faster we develop something more than the garbage on Market, the better off Android will be in the future.

    I ask, lead the way. Expand on your dissection of the Honeycomb Gallery. Teach us.

    Thanks,

    Frank Denton

  2. mobidev says:

    Thanks for your comments in advance!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s