1 /* 2 * Copyright 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.tabcompat.lib; 18 19 import android.os.Build; 20 import android.os.Bundle; 21 import android.support.v4.app.FragmentActivity; 22 23 /** 24 * Convenience helper to build a set of tabs for a {@link TabCompatActivity}. To use this class, 25 * extend {@link TabCompatActivity} and: 26 * 27 * Call {@link TabCompatActivity#getTabHelper()}, returning a {@link TabHelper}. 28 * 29 * Create a {@link CompatTabListener}. 30 * 31 * Call {@link TabHelper#newTab(String)} to create each tab. 32 * 33 * Call CompatTab.setText().setIcon().setTabListener() to set up your tabs. 34 * 35 * Call {@link TabHelper#addTab(CompatTab)} for each tab, and you're done. 36 */ 37 public abstract class TabHelper { 38 39 protected FragmentActivity mActivity; 40 TabHelper(FragmentActivity activity)41 protected TabHelper(FragmentActivity activity) { 42 mActivity = activity; 43 } 44 45 /** 46 * Factory method for creating TabHelper objects for a given activity. Depending on which device 47 * the app is running, either a basic helper or Honeycomb-specific helper will be returned. 48 * Don't call this yourself; the TabCompatActivity instantiates one. Instead call 49 * TabCompatActivity.getTabHelper(). 50 */ createInstance(FragmentActivity activity)51 public static TabHelper createInstance(FragmentActivity activity) { 52 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 53 return new TabHelperHoneycomb(activity); 54 } else { 55 return new TabHelperEclair(activity); 56 } 57 } 58 59 /** 60 * Create a new tab. 61 * 62 * @param tag A unique tag to associate with the tab and associated fragment 63 * @return CompatTab for the appropriate android version 64 */ newTab(String tag)65 public CompatTab newTab(String tag) { 66 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 67 return new CompatTabHoneycomb(mActivity, tag); 68 } else { 69 return new CompatTabEclair(mActivity, tag); 70 } 71 } 72 addTab(CompatTab tab)73 public abstract void addTab(CompatTab tab); 74 onSaveInstanceState(Bundle outState)75 protected abstract void onSaveInstanceState(Bundle outState); 76 onRestoreInstanceState(Bundle savedInstanceState)77 protected abstract void onRestoreInstanceState(Bundle savedInstanceState); 78 setUp()79 protected abstract void setUp(); 80 } 81