1 package ${packageName}; 2 3 import android.app.ActionBar; 4 import android.app.FragmentTransaction; 5 import android.os.Bundle; 6 import android.support.v4.app.Fragment; 7 import android.support.v4.app.FragmentActivity; 8 import android.support.v4.app.NavUtils; 9 import android.view.Gravity; 10 import android.view.LayoutInflater; 11 import android.view.Menu; 12 import android.view.MenuItem; 13 import android.view.View; 14 import android.view.ViewGroup; 15 import android.widget.TextView; 16 17 public class ${activityClass} extends FragmentActivity implements ActionBar.TabListener { 18 19 /** 20 * The serialization (saved instance state) Bundle key representing the 21 * current tab position. 22 */ 23 private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item"; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.${layoutName}); 29 30 // Set up the action bar to show tabs. 31 final ActionBar actionBar = getActionBar(); 32 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 33 <#if parentActivityClass != ""> 34 // Show the Up button in the action bar. 35 actionBar.setDisplayHomeAsUpEnabled(true); 36 </#if> 37 38 // For each of the sections in the app, add a tab to the action bar. 39 actionBar.addTab(actionBar.newTab().setText(R.string.title_section1).setTabListener(this)); 40 actionBar.addTab(actionBar.newTab().setText(R.string.title_section2).setTabListener(this)); 41 actionBar.addTab(actionBar.newTab().setText(R.string.title_section3).setTabListener(this)); 42 } 43 44 @Override 45 public void onRestoreInstanceState(Bundle savedInstanceState) { 46 // Restore the previously serialized current tab position. 47 if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) { 48 getActionBar().setSelectedNavigationItem( 49 savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM)); 50 } 51 } 52 53 @Override 54 public void onSaveInstanceState(Bundle outState) { 55 // Serialize the current tab position. 56 outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, 57 getActionBar().getSelectedNavigationIndex()); 58 } 59 60 <#include "include_onCreateOptionsMenu.java.ftl"> 61 <#include "include_onOptionsItemSelected.java.ftl"> 62 63 @Override 64 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 65 // When the given tab is selected, show the tab contents in the 66 // container view. 67 Fragment fragment = new DummySectionFragment(); 68 Bundle args = new Bundle(); 69 args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1); 70 fragment.setArguments(args); 71 getSupportFragmentManager().beginTransaction() 72 .replace(R.id.container, fragment) 73 .commit(); 74 } 75 76 @Override 77 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 78 } 79 80 @Override 81 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 82 } 83 84 <#include "include_DummySectionFragment.java.ftl"> 85 86 } 87