9
I'm using actionbar tabs because I need the navigation elements to be on every page. I'm using ActionBarSherlock for backwards compatibility (minimum API 8, target API 17). My MainActivity extends SherlockFragmentActivity. In my onCreate() for that, I have
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(true);
Tab tab1 = actionBar.newTab().setText("My Pages")
.setTabListener(new MyPagesFragment());
Tab tab2 = actionBar.newTab().setText("Search")
.setTabListener(new SearchFragment());
Tab tab3 = actionBar.newTab().setText("About")
.setTabListener(new AboutFragment());
// Start with the second tab selected.
actionBar.addTab(tab1, 0, false);
actionBar.addTab(tab2, 1, true);
actionBar.addTab(tab3, 2, false);
All the tab fragments are SherlockListFragments that implement ActionBar.TabListener, and do this
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(android.R.id.content, this, "mypages");
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(this);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// Force a complete reload.
onTabSelected(tab, ft);
}
The search page has an EditText and uses its value in an AsyncTask to fetch data from an API and add it to an SQLite database, before calling
((MainActivity) getActivity()).showDetailView(responseCode);
to show the details, which is a method in my MainActivity as follows:
protected void showDetailView(long codeID) {
SherlockFragment detailFragment = new DetailFragment();
Bundle args = new Bundle();
args.putLong("codeID", codeID);
detailFragment.setArguments(args);
FragmentManager manager =