Question

The problem is, that if I press the hardware button for the OptionsMenu, the app freezes and after a half second the fragment view disappears. There is no LogCat error, this is some kind of confusing me. The menu is not working in one out of six fragments. Same problem happens in every fragment.

I want to create an OptionsMenu with the following code. The OptionsMenu is created in the activity, where the fragments get shown.

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();

    inflater.inflate(R.menu.main_activity_actions, menu);

    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
    // then it has handled the app icon touch event

    if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    //return super.onOptionsItemSelected(item);

    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_add:
            add();
            return true;
        case R.id.action_logoff:
            Intent i = new Intent(this, Login.class);
            startActivity(i);
            return true;
        default:
            return super.onOptionsItemSelected(item);

    }


}

XML: main_activity_actions.xml

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/action_add"
        android:title="@string/action_add"
        android:showAsAction="ifRoom" />

    <item android:id="@+id/action_logoff"
        android:title="Ausloggen"
        android:showAsAction="ifRoom"/>

</menu>

I tried switching around the return statment to true and super.onOptionsItemSelected(item); but nothing changes. The App is developed for Android 2.3

Was it helpful?

Solution

Maybe there are some problems with the hardwarebutton and Fragments in Android version < 3.0.
Here is a guide, how to implement a Actionbar with a softwarebased solution: http://hmkcode.com/add-actionbar-to-android-2-3-x/

OTHER TIPS

Try to return super.onCreateOptionsMenu(menu) instead true

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top