Since you have a paramterized Tabs
constructor, the default constructor is not available to the system to create an instance of it. To overcome this issue, you need to explicitly define a default constructor.
But there is another issue with the current implementation:
ActionBar.Tab tab = actionBar.newTab().setText(R.string.LabelSearchTabTitle).setTabListener((ActionBar.TabListener)new Tabs(this, LandingSearch.class, TAG ));
The current approach results in creating a instance of Activity for each tab added. Instead current instance i.e. this
need to be passed to setTabListener
, which is not possible.
Solution :
The cleaner approach would be instead of Tabs
implementing ActionBar.TabListener
, define a inner class in Tabs
which will implement ActionBar.TabListener
and explicitly set an instance of it on each tab that will be created.
Here is the code:
import android.app.ActionBar.Tab;
import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
protected static final String TAG = MainActivity.class.toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
bar.addTab(bar.newTab()
.setText("Search")
.setTabListener(new TabListener<LandingSearch>(
this, "search", LandingSearch.class)));
bar.addTab(bar.newTab()
.setText("Book")
.setTabListener(new TabListener<LandingBook>(
this, "book", LandingBook.class)));
}
public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
private final FragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;
private final Bundle mArgs;
private Fragment mFragment;
public TabListener(FragmentActivity activity, String tag, Class<T> clz) {
this(activity, tag, clz, null);
}
public TabListener(FragmentActivity activity, String tag, Class<T> clz, Bundle args) {
mActivity = activity;
mTag = tag;
mClass = clz;
mArgs = args;
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached()) {
FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
ft.detach(mFragment);
ft.commit();
}
}
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
// Since ActionBar.TabListener needs android.app.FragmentTransaction, define the method signature with it, but don't use it
// Instead use the support fragment manager
FragmentTransaction fft = mActivity.getSupportFragmentManager().beginTransaction();
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
fft.add(android.R.id.content, mFragment, mTag);
} else {
fft.attach(mFragment);
}
// Don't forget to call commit, as we are not using FragmentTransaction passed by ActionBar.
fft.commit();
}
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
FragmentTransaction fft = mActivity.getSupportFragmentManager().beginTransaction();
if (mFragment != null) {
fft.detach(mFragment);
}
fft.commit();
}
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show();
}
}
public static class LandingSearch extends Fragment
{
protected static final String TAG = LandingSearch.class.toString();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.landing_search, container, false);
setHasOptionsMenu(true);
// Other code.
return v;
}
}
public static class LandingBook extends Fragment
{
protected static final String TAG = LandingBook.class.toString();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.landing_book, container, false);
setHasOptionsMenu(true);
// Other code
return v;
}
}
}
Look at this Android example.