| page.title=Abstracting the New APIs |
| parent.title=Creating Backward-Compatible UIs |
| parent.link=index.html |
| |
| trainingnavtop=true |
| next.title=Proxying to the New APIs |
| next.link=new-implementation.html |
| |
| @jd:body |
| |
| <div id="tb-wrapper"> |
| <div id="tb"> |
| |
| <h2>This lesson teaches you to:</h2> |
| <ul> |
| <li><a href="#prepare-abstraction">Prepare for Abstraction</a></li> |
| <li><a href="#create-abstract-tab">Create an Abstract Tab Interface</a></li> |
| <li><a href="#abstract-actionbar-tab">Abstract ActionBar.Tab</a></li> |
| <li><a href="#abstract-actionbar-methods">Abstract ActionBar Tab Methods</a></li> |
| </ul> |
| |
| <h2>You should also read</h2> |
| <ul> |
| <li><a href="{@docRoot}guide/topics/ui/actionbar.html">Action Bar</a></li> |
| <li><a href="{@docRoot}guide/topics/ui/actionbar.html#Tabs">Action Bar Tabs</a></li> |
| </ul> |
| |
| <h2>Try it out</h2> |
| |
| <div class="download-box"> |
| <a href="http://developer.android.com/shareables/training/TabCompat.zip" |
| class="button">Download the sample app</a> |
| <p class="filename">TabCompat.zip</p> |
| </div> |
| |
| </div> |
| </div> |
| |
| <p>Suppose you want to use <a href="{@docRoot}guide/topics/ui/actionbar.html#Tabs">action bar tabs</a> as the primary form of top-level navigation in your application. Unfortunately, the {@link android.app.ActionBar} APIs are only available in Android 3.0 or later (API level 11+). Thus, if you want to distribute your application to devices running earlier versions of the platform, you need to provide an implementation that supports the newer API while providing a fallback mechanism that uses older APIs.</p> |
| |
| <p>In this class, you build a tabbed user interface (UI) component that uses abstract classes with version-specific implementations to provide backward-compatibility. This lesson describes how to create an abstraction layer for the new tab APIs as the first step toward building the tab component.</p> |
| |
| <h2 id="prepare-abstraction">Prepare for Abstraction</h2> |
| |
| <p><a href="http://en.wikipedia.org/wiki/Abstraction_(computer_science)">Abstraction</a> in the Java programming language involves the creation of one or more interfaces or abstract classes to hide implementation details. In the case of newer Android APIs, you can use abstraction to build version-aware components that use the current APIs on newer devices, and fallback to older, more compatible APIs on older devices.</p> |
| |
| <p>When using this approach, you first determine what newer classes you want to be able to use in a backward compatible way, then create abstract classes, based on the public interfaces of the newer classes. In defining the abstraction interfaces, you should mirror the newer API as much as possible. This maximizes forward-compatibility and makes it easier to drop the abstraction layer in the future when it is no longer necessary.</p> |
| |
| <p>After creating abstract classes for these new APIs, any number of implementations can be created and chosen at runtime. For the purposes of backward-compatibility, these implementations can vary by required API level. Thus, one implementation may use recently released APIs, while others can use older APIs.</p> |
| |
| <h2 id="create-abstract-tab">Create an Abstract Tab Interface</h2> |
| |
| <p>In order to create a backward-compatible version of tabs, you should first determine which features and specific APIs your application requires. In the case of top-level section tabs, suppose you have the following functional requirements:</p> |
| |
| <ol> |
| <li>Tab indicators should show text and an icon.</li> |
| <li>Tabs can be associated with a fragment instance.</li> |
| <li>The activity should be able to listen for tab changes.</li> |
| </ol> |
| |
| <p>Preparing these requirements in advance allows you to control the scope of your abstraction layer. This means that you can spend less time creating multiple implementations of your abstraction layer and begin using your new backward-compatible implementation sooner.</p> |
| |
| <p>The key APIs for tabs are in {@link android.app.ActionBar} and {@link android.app.ActionBar.Tab ActionBar.Tab}. These are the APIs to abstract in order to make your tabs version-aware. The requirements for this example project call for compatibility back to Eclair (API level 5) while taking advantage of the new tab features in Honeycomb (API Level 11). A diagram of the class structure to support these two implementations and their abstract base classes (or interfaces) is shown below.</p> |
| |
| <img src="{@docRoot}images/training/backward-compatible-ui-classes.png" |
| alt="Class diagram of abstract base classes and version-specific implementations." id="figure-classes"> |
| |
| <p class="img-caption"><strong>Figure 1.</strong> Class diagram of abstract base classes and version-specific implementations.</p> |
| |
| <h2 id="abstract-actionbar-tab">Abstract ActionBar.Tab</h2> |
| |
| <p>Get started on building your tab abstraction layer by creating an abstract class representing a tab, that mirrors the {@link android.app.ActionBar.Tab ActionBar.Tab} interface:</p> |
| |
| <pre> |
| public abstract class CompatTab { |
| ... |
| public abstract CompatTab setText(int resId); |
| public abstract CompatTab setIcon(int resId); |
| public abstract CompatTab setTabListener( |
| CompatTabListener callback); |
| public abstract CompatTab setFragment(Fragment fragment); |
| |
| public abstract CharSequence getText(); |
| public abstract Drawable getIcon(); |
| public abstract CompatTabListener getCallback(); |
| public abstract Fragment getFragment(); |
| ... |
| } |
| </pre> |
| |
| <p>You can use an abstract class instead of an interface here to simplify the implementation of common features such as association of tab objects with activities (not shown in the code snippet).</p> |
| |
| <h2 id="abstract-actionbar-methods">Abstract ActionBar Tab Methods</h2> |
| |
| <p>Next, define an abstract class that allows you to create and add tabs to an activity, like {@link android.app.ActionBar#newTab ActionBar.newTab()} and {@link android.app.ActionBar#addTab ActionBar.addTab()}:</p> |
| |
| <pre> |
| public abstract class TabHelper { |
| ... |
| |
| public CompatTab newTab(String tag) { |
| // This method is implemented in a later lesson. |
| } |
| |
| public abstract void addTab(CompatTab tab); |
| |
| ... |
| } |
| </pre> |
| |
| <p>In the next lessons, you create implementations for <code>TabHelper</code> and <code>CompatTab</code> that work across both older and newer platform versions.</p> |