Menu Item InstantiatingTabListener - Android Fragment -
<p><img src="http://i.stack.imgur.com/KDFyU.png" alt="problem in menu items not showing text and icon"> <a href="http://i.stack.imgur.com/KDFyU.png" rel="nofollow">1</a>: <a href="http://i.stack.imgur.com/WYniI.png" rel="nofollow">http://i.stack.imgur.com/WYniI.png</a></p>

<p>I have written code for showing tab menu in my app. it works fine but in some mobiles tab does not show text and icon as shown in attached image. I am using Fragments.</p>

<pre><code> public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    TabHelper tabHelper = getTabHelper();

    CompatTab menuTab = tabHelper.newTab("menu").setText(R.string.tab_section1).setIcon(
            R.drawable.home_icon).setTabListener(
            new InstantiatingTabListener(this, MenuFragment.class));

    tabHelper.addTab(menuTab);
</code></pre>

<p>After lots of triage i found below code has issues</p>

<pre><code>@Override
public boolean onCreateOptionsMenu(Menu menu) {

    super.onCreateOptionsMenu(menu);
    //nothing xml written for menu 
}
</code></pre>

<p>So i tried to add below code</p>

<pre><code>  @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.menufav:
            Intent menuintent = new Intent(this, FavouritesFragment.class);
            startActivity(menuintent);
            break;

        default:
            break;
    }
    return super.onOptionsItemSelected(item);
}
</code></pre>

<p>BUT SINCE IT IS FRAGMENT WE CAN NOT USE INTENT. IT THROWS CASTING ERROR.</p>

<p>Now how do I solve this issue? I have below class for click listener. i want to make use of it.</p>

<pre><code> public static class InstantiatingTabListener implements CompatTabListener {

    private final TabCompatActivity mActivity;
    private final Class mClass;

    /**
     * Constructor used each time a new tab is created.
     * 
     * @param activity The host Activity, used to instantiate the fragment
     * @param cls The class representing the fragment to instantiate
     */
    public InstantiatingTabListener(TabCompatActivity activity, Class&lt;? extends Fragment&gt; cls) {
        mActivity = activity;
        mClass = cls;
    }

    /* The following are each of the ActionBar.TabListener callbacks */
    @Override
    public void onTabSelected(CompatTab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
        Fragment fragment = tab.getFragment();
        if (fragment == null) {
            // If not, instantiate and add it to the activity
            fragment = Fragment.instantiate(mActivity, mClass.getName());
            tab.setFragment(fragment);
            ft.add(android.R.id.tabcontent, fragment, tab.getTag());
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(fragment);
        }
    }

    @Override
    public void onTabUnselected(CompatTab tab, FragmentTransaction ft) {
        Fragment fragment = tab.getFragment();
        if (fragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(fragment);
        }
    }

    @Override
    public void onTabReselected(CompatTab tab, FragmentTransaction ft) {

    }
}
</code></pre>
