Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Toolbar Not Calling onOptionsItemSelected From Fragments On Backstack

I recently started updating my app to use the new Toolbar component introduced in Android 5.0 in favor of using a custom view over the action bar. I followed the guide here: http://antonioleiva.com/material-design-everywhere/ and adding the Toolbar works fine. The problem is, I am using a navigation structure where I have a MainActivity and replace the content by adding Fragments on to the backstack. I am overriding the onCreateOptionsMenu and onOptionsItemSelected methods in my Fragments to set the menu items in the Toolbar, and the icons change appropriately when I switch Fragments and onOptionsItemSelected is called on the first Fragment, but is not called when I add a Fragment on to the backstack. The onOptionsItemSelected function in the MainActivity is not even called, so the event isn't being consumed by the Activity. I have also tried just replacing the Fragment without adding it to the backstack, but onOptionsItemSelected is still not called. What am I missing to get onOptionsItemSelected to be called once I change the content Fragment? Relevant code is posted below.

App Theme:

<style name="AppThemeLight" parent="@style/Theme.AppCompat.Light">
    <item name="actionMenuTextColor">@color/white</item>
    <item name="android:windowDisablePreview">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="android:windowActionBar">false</item>
</style>

Adding Toolbar in MainActivity:

Toolbar toolbar = (Toolbar)findViewById( R.id.toolbar );
if (toolbar != null) {
    setSupportActionBar( toolbar );
    getSupportActionBar().setDisplayHomeAsUpEnabled( true );
    toolbar.setNavigationIcon( R.drawable.toolbar_icon_menu );
}

Menu functions in MainActivity:

@Override
public boolean onCreateOptionsMenu( Menu menu ) {
    Log.v( "Main", "onCreateOptionsMenu" );
    return super.onCreateOptionsMenu( menu );
}

@Override
public boolean onOptionsItemSelected( MenuItem item ) {
    Log.v( "Main", "onOptionsItemSelected" );
    return super.onOptionsItemSelected( item );
}

Top level Fragment Menu functions:

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

@Override
public boolean onOptionsItemSelected( MenuItem item ) {
    switch (item.getItemId()) {
        case R.id.miOptions:
            onOptions();
            return true;
        default:
            return super.onOptionsItemSelected( item );
    }
}

Menu functions in Fragment on backstack

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

@Override
public boolean onOptionsItemSelected( MenuItem item ) {
    Log.v( "User", "onOptionsItemSelected" );
    switch (item.getItemId()) {
        case R.id.miUserShare:
            onShareUser();
            return true;
        case R.id.miUserEdit:
            onEditUserProfile();
            return true;
        default:
            return super.onOptionsItemSelected( item );
    }
}
like image 634
sven Avatar asked Oct 23 '14 14:10

sven


1 Answers

Solved the problem by chance while making changes to other Fragments and noticing that onOptionsItemSelected was called from Fragments with simpler layouts. It turns out that for some reason having a ScrollView as the top level component for a Fragment's layout interferes with the Toolbar receiving touch events. By wrapping the ScrollView in an extra RelativeLayout (any container layout would probably work) onOptionsItemSelected is able to be called. I'm guessing that it has something to do with the fact that the Toolbar component is now part of the view hierarchy - can't think of why else adding a wrapper for the ScrollView would fix the problem. If anyone could help explain this weird behavior, it would be appreciated.

like image 126
sven Avatar answered Sep 19 '22 15:09

sven