Skip to content

Instantly share code, notes, and snippets.

@kingori
Created June 12, 2012 16:41
Show Gist options
  • Save kingori/2918614 to your computer and use it in GitHub Desktop.
Save kingori/2918614 to your computer and use it in GitHub Desktop.
ActionBar Tab with Fragment
package com.proto.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockFragmentActivity;
public class MainActivity extends RoboSherlockFragmentActivity {
private static final String TAB_INDEX = "tab_index";
private static final String FRAGMENT1_ID = "aaa";
private static final String FRAGMENT2_ID = "bbb";
int savedTabIndex = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(LOG_TAG, "MainActivity.onCreate: start...");
setContentView(R.layout.main);
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowTitleEnabled(false);
TabContentFragment tabFragment1 = TabContentFragment.getInstance(FRAGMENT1_ID);
TabContentFragment tabFragment2 = TabContentFragment.getInstance(FRAGMENT2_ID);
bar.addTab(bar.newTab().setText(FRAGMENT1_ID).setTabListener(new CustomTabListener(tabFragment1)), false);
bar.addTab(bar.newTab().setText(FRAGMENT2_ID).setTabListener(new CustomTabListener(tabFragment2)), false);
if (savedInstanceState != null) {
//restore saved tab index
savedTabIndex = savedInstanceState.getInt(TAB_INDEX, 0);
}
}
@Override
protected void onResume() {
super.onResume();
//select tab
//as saved fragment is created after onCreate(), should call getTabAt().select in onResume()
getSupportActionBar().getTabAt(savedTabIndex).select();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
//save current tab index
outState.putInt(TAB_INDEX, getSupportActionBar().getSelectedTab().getPosition());
super.onSaveInstanceState(outState);
}
private class CustomTabListener implements TabListener {
private TabContentFragment f;
public CustomTabListener(TabContentFragment tabFragment1) {
this.f = tabFragment1;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction fakeFt) {
FragmentManager fragMgr = MainActivity.this.getSupportFragmentManager();
FragmentTransaction ft = fragMgr.beginTransaction();
String fragmentId = f.getText();
Fragment foundF = fragMgr.findFragmentByTag(fragmentId); //try to find current fragment
if (foundF == null) {
Log.d(LOG_TAG,
"MainActivity.CustomTabListener.onTabSelected: failed to find fragment by tag. add to ft "
+ fragmentId);
ft.add(android.R.id.content, f, fragmentId);
} else {
if (f == foundF) {
Log.d(LOG_TAG,
"MainActivity.CustomTabListener.onTabSelected: found fragment by tag. just show it: "
+ f.getText());
ft.show(f);
} else {
Log.d(LOG_TAG, "MainActivity.CustomTabListener.onTabSelected: remove old fragment and add new "+fragmentId);
ft.remove(foundF);
ft.add(android.R.id.content, f, fragmentId);
}
}
ft.commit();
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.hide(f);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
//nothing to do
}
}
private static final String LOG_TAG = "main";
public static class TabContentFragment extends Fragment {
private String mText;
private TextView tv;
public static TabContentFragment getInstance(String text) {
TabContentFragment f = new TabContentFragment();
f.mText = text;
return f;
}
public String getText() {
return mText;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragView = inflater.inflate(R.layout.tab_fragment, container, false);
tv = ((TextView) fragView.findViewById(R.id.tab_tv));
tv.setText(mText);
Log.d(LOG_TAG, "MainActivity.TabContentFragment.onCreateView:");
return fragView;
}
@Override
public void onDestroy() {
Log.d(LOG_TAG, "MainActivity.TabContentFragment.onDestroy:");
super.onDestroy();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment