Search code examples
javaandroidandroid-optionsmenu

Android Action Bar options menu display issue


Okay, so i got a small problem that i really don't know how to fix this.

So i have an application where i use my own style for an Action Bar

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionModeOverlay">true</item>
        <item name="actionBarStyle">@style/AppTheme.ActionBar</item>
        <item name="android:textColorSecondary">@color/action_bar_bg</item>
    </style>

    <style name="AppTheme.ActionBar" parent="Widget.AppCompat.ActionBar">
        <item name="height">30dp</item>
        <item name="background">@color/action_bar_bg</item>
    </style>
</resources>

The menu that is displayed

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <item android:id="@+id/sortByStatus"
        android:title="@string/sort_by_status"
        app:showAsAction="never" />

    <item android:id="@+id/sortByName"
        android:title="@string/sort_by_name"
        app:showAsAction="never" />

    <item android:id="@+id/sortByCompany"
        android:title="@string/sort_by_company"
        app:showAsAction="never" />
</menu>

This works great for almost all places, except when i try to display my options menu. I have added the options menu and the handler for it, but when i click on the options menu then the default top icons (from the notification bar) also shows inside my options menu, and i don't want those to show there. It also starts showing the default bottom navigation buttons for Sony phones.

example of the problem1 example of the problem2

Enabling the menu from onResume

@Override
public void onResume() {
    super.onResume();

    AppCompatActivity appCompatActivity = (AppCompatActivity) getActivity();
    if (appCompatActivity != null && appCompatActivity.getSupportActionBar() != null)
        appCompatActivity.getSupportActionBar().setTitle(getResources().getString(R.string.presense_status));

    setHasOptionsMenu(true);
    ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

and then the menu onCreate

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_presense_status, menu);
}

and then the handler for selected item

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            getActivity().getFragmentManager().popBackStackImmediate();
            break;

        case R.id.sortByStatus:
            if (null != mDataSource) {
                // update data list
                reloadData();
            }
            break;

        case R.id.sortByName:
            if (null != mDataSource) {
                // update data list
                reloadData();
            }
            break;

        case R.id.sortByCompany:
            if (null != mDataSource) {
                // update data list
                reloadData();
            }
            break;

        default:
            super.onOptionsItemSelected(item);
    }

    return true;
}

I really can't see where the problem is coming from or how to remove it so that this does not happen. This happens inside a Fragment that is started by my main Activity, meaning i only have one Activity that spawns different fragments for the different in-app tasks. Any help or thought would be greatly appreciated.


Solution

  • I still don't really know why the notification bar was affected by what i did, but forcing the application to fullscreen in this view stops the problem, so i guess that's an acceptable workaround.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }