Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onCreateOptionsMenu not being called on FragmentActivity when run on phone version

Tags:

I create an app that supports both phone and tablet version so i use the android-support-v4.jar library. My activity extends the FragmentActivity and override the onCreateOptionsMenu(Menu menu). This works fine on tablet, the onCreateOptionsMenu being called correctly but it doesn't work on phone, onCreateOptionsMenu never get called. How to resolve this?

Note: i use <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="12"/> on Manifest file.

like image 465
Lorensius W. L. T Avatar asked Aug 04 '11 06:08

Lorensius W. L. T


People also ask

What is onCreateOptionsMenu () in android?

If you've developed for Android 3.0 and higher, the system calls onCreateOptionsMenu() when starting the activity, in order to show items to the app bar.

What is use of Method public boolean onCreateOptionsMenu menu menu?

Find the onCreateOptionsMenu(Menu menu) method which needs to override in Activity class. This creates menu and returns Boolean value. inflate inflates a menu hierarchy from XML resource.

How do you call onCreateOptionsMenu?

In case of ICS and Honeycomb onCreateOptionsMenu() is called after onCreate() and onPostCreate() while in Gingerbread and earlier versions it is called after onCreate() but before onPostCreate() . Save this answer.

What is a FragmentActivity?

A FragmentActivity is a subclass of Activity that was built for the Android Support Package. The FragmentActivity class adds a couple new methods to ensure compatibility with older versions of Android, but other than that, there really isn't much of a difference between the two.


1 Answers

You should consider from your Fragment code:

1) Implementing onCreateOptionsMenu(Menu menu, MenuInflater inflater)

2) Calling setHasOptionsMenu

3) And also implementing onOptionsItemSelected(MenuItem item)

Then you will be ok on both the phone and tablet.

like image 154
PJL Avatar answered Oct 01 '22 05:10

PJL