onCreateOptionsMenu not triggered in fragment

1.7k views Asked by At

I encounter an issue with one of my fragments which does not trigger at all onCreateOptionsMenu() unlike my other fragments. There is no exception neither or any particular trace and I use the setHasOptionsMenu(true) method.

package fr.test.test.fragment;

import android.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

import fr.test.test.MainActivity;
import fr.test.test.listener.FragmentTabListener;
import fr.test.test.R;

public class TestFragment extends Fragment {

    private ActionBar.Tab Tab1, Tab2;
    private Fragment fragmentTab1;
    private Fragment fragmentTab2;
    private ActionBar actionBar;

    public TestFragment() {

        fragmentTab1 = new Fragment1();
        fragmentTab2 = new Fragment2();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_messages, container, false);
        setHasOptionsMenu(true);

        actionBar = getActivity().getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        Tab1 = actionBar.newTab().setText(R.string.title_fragment1);
        Tab2 = actionBar.newTab().setText(R.string.title_fragment2);

        Tab1.setTabListener(new FragmentTabListener(getActivity(), fragmentTab1));
        Tab2.setTabListener(new FragmentTabListener(getActivity(), fragmentTab2));

        actionBar.removeAllTabs();
        actionBar.addTab(Tab1);
        actionBar.addTab(Tab2);

        return rootView;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        System.out.println("onCreateOptionsMenu TRIGGER ME PLZ :( ");
        inflater.inflate(R.menu.global, menu);
    }
}

The problem does not come from my menu because my other fragments triggers this event, that's why I do not understand a thing on this case. I use exactly the same logic but with the standart navigation from the ActionBar instead of tabs. Any ideas ?

Thanks for watching,

Regards.

0

There are 0 answers