vendredi 8 mai 2015

calling invalidateOptionsMenu() causes SearchView to stop working

I am working with a DrawerLayout and I am using invalidateOptionsMenu() whenever it is opened or closed. The trouble is that after opening and closing the DrawerLayout the SearchView stops working.

Class1:

private DrawerLayout DrawerLayout;
private ListView DrawerList;
private ActionBarDrawerToggle DrawerToggle;
private CharSequence DrawerTitle;
private CharSequence Title;
.....

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
        R.drawable.menu, R.string.app_name, R.string.hello) {
    public void onDrawerClosed(View view) {
        getActionBar().setTitle(Title);
        invalidateOptionsMenu(); 
        // onPrepareOptionsMenu()
    }

    public void onDrawerOpened(View drawerView) {
        getActionBar().setTitle(DrawerTitle);
        invalidateOptionsMenu(); 
        // onPrepareOptionsMenu()
    }
};

mDrawerLayout.setDrawerListener(DrawerToggle);

SearchView defined Class2:

@Override
public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    getActivity().invalidateOptionsMenu();
    // onPrepareOptionsMenu()
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.actionbar, menu);
    searchView = (SearchView) menu.findItem(R.id.search).getActionView();
}

Neither class defines onPrepareOptionsMenu()

Specifically this is what stops working:

@Override
public boolean onQueryTextChange(String newText) {
   adapter.getFilter().filter(newText);
    return false;
}

Where adapter is the adapter for a ListView and getFilter goes to a baseadapter class which implements filterable.

Aucun commentaire:

Enregistrer un commentaire