0
votes

I have a project where i use Android Support Library to include the ActionBar for pre 3.0-devices.

I also have Tabs on my project. The Tabs are Fragments. This way, I extend every Tab from the Class android.support.v4.app.Fragment from Support Library.

However, I have a problem with pre 3.0-devices. There are no action bar being displayed. So, there are also no tabs.

I can't find the solution....

So my "main-activity" (which handles the ActionBar with the tabs) looks this like:

public class Hauptmenue_extended extends ActionBarActivity implements
        OnClickListener {
actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Tab tabB = actionBar.newTab();
    tabB.setText("Home");
    tabB.setIcon(R.drawable.icon_home);
    tabB.setTabListener(new TabListener<Startmenue_activity>(this, "Start",
            Startmenue_activity.class));
    actionBar.addTab(tabB);

    Tab tabA = actionBar.newTab();
    tabA.setText("");
    tabA.setIcon(R.drawable.icon_nachrichten_sel);
    tabA.setTabListener(new TabListener<Nachrichten_activity>(this,
            "Nachrichten", Nachrichten_activity.class));
    actionBar.addTab(tabA);

    Tab tabC = actionBar.newTab();
    tabC.setText("");
    tabC.setIcon(R.drawable.icon_favoriten);
    tabC.setTabListener(new TabListener<Favoriten_activity>(this,
            "Favoriten", Favoriten_activity.class));
    actionBar.addTab(tabC);

And my TabListener looks like this:

public static class TabListener<T extends Fragment> implements
        ActionBar.TabListener {

    private final FragmentActivity myActivity;
    private final String myTag;
    private final Class<T> myClass;

    public TabListener(FragmentActivity activity, String tag, Class<T> cls) {
        myActivity = activity;
        myTag = tag;
        myClass = cls;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        Fragment myFragment = myActivity.getSupportFragmentManager()
                .findFragmentByTag(myTag);

        // Check if the fragment is already initialized
        if (myFragment == null) {
            // If not, instantiate and add it to the activity
            myFragment = Fragment
                    .instantiate(myActivity, myClass.getName());
            ft.add(android.R.id.content, myFragment, myTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(myFragment);
        }

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {

        Fragment myFragment = myActivity.getSupportFragmentManager()
                .findFragmentByTag(myTag);

        if (myFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(myFragment);
        }

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

}

And every Fragment or Tab looks like this:

public class Nachrichten_activity extends Fragment implements
    OnChildClickListener, OnClickListener { ...

Does anyone have an idea? :)

1
What about the application/activity theme in the manifest? - f2prateek
Why are you implementing an OnClickListener in your Activity? - wyoskibum
I use the folowing Theme: <activity android:name="standard.temp.Hauptmenue_extended" android:theme="@style/Theme.Base.AppCompat.Light.DarkActionBar" android:label="@string/title_activity_hauptmenue_extended" android:screenOrientation="portrait" > </activity> - Maximus1809
The OnClickListener is a part of old code. But this doesnt solve the problem? - Maximus1809
No idea...? I cant solve the problem .. - Maximus1809

1 Answers

0
votes

Some pointers, can't know for sure what will fix your problem

  • use @style/Theme.AppCompat.Light (what is .Base in your example?)
  • Don't name your fragments "_activity", it is only confusing.
  • Due to a bug in support lib, pre ICS use R.id.action_bar_activity_content instead of android.R.id.content . This will be fixed in later versions.
  • use ft.replace() in onTabSelected