2

Colleagues, MainActivity class is derived from FragmentActivity, and for some reason activity's onCreateOptionsMenu() is not getting called. I have the first breakpoint in onCreate(), which is getting triggered, and the second one in onCreateOptionsMenu(), which is not getting triggered.

import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

    private FragmentPagerAdapter m_fragmentPagerAdapter;
    private ViewPager m_viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create the adapter that will return a fragment for each of the  primary sections of the activity.
        m_fragmentPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        m_viewPager = (ViewPager) findViewById(R.id.container);
        m_viewPager.setAdapter(m_fragmentPagerAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);      // Inflate the menu; this adds items to the action bar if it is present.
        return true;
    }
}

Fragments in this app don't have their own menus. One menu belonging to the activity "covers" everything.

Theme is Holo.Light

What prevent onCreateOptionsMenu() from getting called? What am I missing?

Nick Alexeev
  • 1,688
  • 2
  • 22
  • 36
  • Have you tried extending `AppCompatActivity` instead? That will add a toolbar that displays the menu. Note you might have to change your theme in this case. – George Mulligan Jan 27 '16 at 23:09
  • @George I know that I'm targeting only the newer versions of Android (from 4.3 onward). As far as I understand, AppCompat enables older versions of the API to do things that have been introduced in newer versions. I feel, however, that AppCompat is a patch that I should be able to do without. (Of course, it's possible that my understanding about the history and purpose of AppCompat is off.) Changing the base class to `AppCompatActivity` did bring the menu, though. – Nick Alexeev Jan 28 '16 at 03:27

3 Answers3

2

Double-check that you have really set the app theme to Holo.Light. I can't explain what is causing the problem, but I was able to reproduce it. After experimenting with changes between FragmentActivity and AppCompatActivity, I accidentally ran with FragmentActivity and theme set to Theme.AppCompat.Light. This created the behavior you are seeing, with onCreateOptionsMenu() not called.

As a side issue, you should fix your onCreateOptionsMenu() to call through to super as noted in the documentation: "Deriving classes should always call through to the base implementation".

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • Thanks. Your hunch that I haven't actually set the app theme to `Holo.Light` was correct. – Nick Alexeev Jan 28 '16 at 08:06
  • In the process of blundering though all this, I've come across two samples that demonstrate fragments. [This one from Aug 2012](http://www.mysamplecode.com/2012/08/android-fragment-example.html) derives from `Activity` and doesn't use legacy support libraries. [This one from Oct 2013](http://www.codepuppet.com/2013/10/06/using-fragments-in-android-with-fragmentactivity/) uses `FragmentActivity`. Since `Activity` seems to be able to work with fragments, is it always necessary to use `FragmentActivity`? (This app of mine will be written from scratch, and I don't have to support ancient devices.) – Nick Alexeev Jan 28 '16 at 08:06
  • @NickAlexeev: You can use `Activity` instead of `FragmentActivity` if you don't need to support devices older than API 11. Devices with API less then 11 are now about 3%, according to this [dashboard](http://developer.android.com/about/dashboards/index.html). – Bob Snyder Jan 28 '16 at 14:19
1

Try:

setHasOptionsMenu();//call it from onCreate(); or onViewCreated();

This method only used in fragment to tell the activity that this fragment has an option menu.

Kosh
  • 6,140
  • 3
  • 36
  • 67
-3

Add setHasOptionsMenu(true); in your onCreate() method.

Stanojkovic
  • 1,612
  • 1
  • 17
  • 24