title: onCreateOptionsMenu called before onCreate android
body: <p>I am beginner to android and i am developing small android application. In my application I am using Sherlock library. My application contains one main activity and two fragments. On of my list fragment structure looks like </p>
  
  <pre><code>public class MyCards extends SherlockListFragment
          {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setHasOptionsMenu(true);
          Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside fragment on create");
  
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside on activity created");
      }
  
      private void displayCards(int type) {
  
          Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside display cards");
  
      }
  
      @Override
      public void onResume() {
          super.onResume();
  
      }
  
      @Override
      public void onPause() {
          super.onPause();
  
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
  
          View view = inflater.inflate(R.layout.mycards, container, false);
  
          View view1 = inflater.inflate(R.layout.empty_card, container, false);
          return view;
  
      }
  
      /*@Override
      public void onPrepareOptionsMenu(Menu menu) {
      super.onPrepareOptionsMenu(menu);
  
      optionsMenu = menu;
      }*/
  
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
          inflater.inflate(R.menu.menu_type2, menu);
  
          Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside on create option menu");
          Spinner spinner = (Spinner) menu.findItem(R.id.cardType)
                  .getActionView();
  
          SpinnerAdapter adapter = ArrayAdapter.createFromResource(getSherlockActivity().getSupportActionBar().getThemedContext(), R.array.card_action_list,android.R.layout.simple_spinner_dropdown_item);
          spinner.setAdapter(adapter); // set the adapter
  
          spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
  
              @Override
              public void onItemSelected(AdapterView&lt;?&gt; parent, View view,
                      int pos, long id) {
                  displayCards(pos);
                  Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside on option selected");
              }
  
              @Override
              public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
                  displayCards(0);
                  Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside on no option selection");
              }
  
          });
  
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          Log.i("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "inside option select listener");
  
      }
  
  }
  </code></pre>
  
  <p>Now my problem is that when I switch fro one fragment to another fragment for first time 
  it gives following log output </p>
  
  <pre><code>01-04 00:54:06.997: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(2575): inside on create option menu
  01-04 00:54:06.997: W/KeyCharacterMap(2575): No keyboard for id -1
  01-04 00:54:06.997: W/KeyCharacterMap(2575): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
  01-04 00:54:07.020: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(2575): inside fragment on create
  01-04 00:54:07.036: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(2575): inside on activity created
  </code></pre>
  
  <p>That mean on create option called before onfragment create and on activity create . and it also giving two warning. This work with out any error. But I am not able to display my list view</p>
  
  <p>Now when I again switch to same fragment(for second time) it gives following log output</p>
  
  <pre><code>02-07 10:05:10.983: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(15844): inside on create option menu
  02-07 10:05:11.023: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(15844): inside fragment on create
  02-07 10:05:11.043: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(15844): inside on activity created
  02-07 10:05:11.113: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(15844): inside display cards
  02-07 10:05:11.133: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(15844): inside on option selected
  </code></pre>
  
  <p>this time it's not giving any warning and working properly. Also displaying my list view.
  But for the first time it's not working properly..
  Another point is that when I ran it on higher version of android its working fine. But If i ran it on lower version of android like 2.3.3  i shows such behavior.. 
  I read about this <a href="https://stackoverflow.com/questions/12374816/listfragment-onprepareoptionsmenu-called-before-oncreate-why-and-how-to-fix-b">ListFragment onPrepareOptionsMenu called before onCreate. Why and How to Fix / Bypass?</a>   But i am not able to solve this problem.</p>
  
  <p>if I define <code>setHasOptionsMenu(true)</code> inside <code>onActivityCreated</code> it gives following output </p>
  
  <pre><code>01-04 03:32:38.622: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(8157): inside fragment on create
  01-04 03:32:38.653: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(8157): inside on activity created
  01-04 03:32:38.692: I/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(8157): inside on create option menu
  01-04 03:32:38.700: W/KeyCharacterMap(8157): No keyboard for id -1
  01-04 03:32:38.700: W/KeyCharacterMap(8157): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
  </code></pre>
  
  <p>I also tried to set setHasOptionsMenu for my fragment where actual fragment transaction takes place like below..</p>
  
  <pre><code>@Override
          public void onTabSelected(Tab tab, FragmentTransaction ft) {
              MyCards mc = new MyCards();
              mc.setHasOptionsMenu(true);
              ft = mActivity.getSupportFragmentManager().beginTransaction();
  
                  mFragment = Fragment.instantiate(mActivity, mClass.getName(),
                          mArgs);
  
                  ft.add(android.R.id.content, mFragment, mTag);
                  ft.commit();
              }
  </code></pre>
  
  <p>and still it giving same problem.</p>
  
  <p>any problem with my code or some thing else?
  So how to solve this problem.
  Need help...
  thank you...</p>