two month ago I released an Android app v14 as MinSDKVersion and v19 as TargetSDKVersion. Everything works fine, beside that some users reports problem with the ActionBar menu items:
They are shown on first app start but vanish when first switching away from a tab.
The MainActivity of the App extends FragmentActivity
and uses ActionBar Tabs to switch between 3 different pages (Fragments):
public class MainActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
...
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager)findViewById(R.id.pager);
viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
getActionBar().setSelectedNavigationItem(position);
}
});
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(Tab tab, android.app.FragmentTransaction arg1) {
viewPager.setCurrentItem(tab.getPosition());
}
public void onTabReselected(Tab arg0, android.app.FragmentTransaction arg1) { }
public void onTabUnselected(Tab arg0, android.app.FragmentTransaction arg1) { }
};
actionBar.addTab(actionBar.newTab()
.setText(R.string.LOC_Common_Overview)
.setTabListener(tabListener));
actionBar.addTab(actionBar.newTab()
.setText(R.string.LOC_Contacts)
.setTabListener(tabListener));
actionBar.addTab(actionBar.newTab()
.setText(R.string.LOC_Messages)
.setTabListener(tabListener));
...
}
}
An example of the menu file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/contacts_action_add"
android:icon="@drawable/image__white__plus"
android:showAsAction="always"
android:title="@string/LOC_Create">
<menu>
<item
android:id="@+id/contacts_action_add_entry"
android:icon="@drawable/image__white__person"
android:showAsAction="always"
android:title="@string/LOC_Contact"/>
<item
android:id="@+id/contacts_action_add_group"
android:icon="@drawable/image__white__persons"
android:showAsAction="always"
android:title="@string/LOC_Group"/>
</menu>
</item>
</menu>
And an example of the Fragments:
public class ContactsFragment extends Fragment {
public ContactsFragment() {
super();
setHasOptionsMenu(true);
}
...
private Menu optionsMenu;
private MenuItem addEntryMenuItem;
private MenuItem addGroupMenuItem;
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("ContactsFragment: onCreateOptionsMenu");
inflater.inflate(R.menu.menu_main__contacts, menu);
optionsMenu = menu;
if (optionsMenu != null) {
Log.d("ContactsFragment: onCreateOptionsMenu - optionsMenu != null");
addEntryMenuItem= optionsMenu.findItem(R.id.contacts_action_add_entry);
addGroupMenuItem= optionsMenu.findItem(R.id.contacts_action_add_group);
}
}
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("ContactsFragment: onOptionsItemSelected");
if (item == addEntryMenuItem) {
...
return true;
} else if (item == addGroupMenuItem) {
...
return true;
}
return false;
}
}
I found several entries that deal with problems with ActionBar items but they all refere to apps with lower base SDKs, prior to 4.0. It seeems that there have been problems when transitioning apps from using the hardware menu button ActionBar menus. Since my app support SDK 14+ only this cannot be the solution in my case.
User logs show, that the onCreateOptionsMenu
method of the fragments is called and that the menues are inflated correctly. So why are the menu items not displayed correctly?
The same code works without any problem on the waste majority of all devices, only a few users are effected. As far as I can tell they all use Android 4.1.2. Beside this I cannot see any similarities in devices, etc.
Any idea how this problem can be solved?
PS: Wether `android:showAsAction="always"`` is used or not for the menu items does not influende the problem.
Seems like you have missed the call setHasOptionsMenu in onCreate() of your Fragment
See this guide