0

I have an activity which is containing fragment, then in the fragment i have toolbar which is containing SearchView. I want to override activity's onBackPressed() to close the searchView's focus. But i didn't found how to get the state of fragment's menu. Here's the code :

MainActivity.java

@Override
public void onBackPressed() {
    if (currentFragment instanceof FragmentListProduct) {
        DrawerLayout drawerLayout = (DrawerLayout) currentFragment.getView().findViewById(R.id.drawer_layout);
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } /*if currentFragment.searchViewIsNotClosed {
            closeSearchView();
        } */ else if (backstackFragment.size() > 0) {
            replaceFragment(null, backstackFragment.pop());
        } else {
            super.onBackPressed();
        }
    } else {
        super.onBackPressed();
    }
}

FragmentListProduct.java

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

    MenuItem searchItem = menu.findItem(R.id.app_bar_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {...});

    super.onCreateOptionsMenu(menu, inflater);
}

So how to get fragment's menu in activity so i can access its state?

zihadrizkyef
  • 1,849
  • 3
  • 23
  • 46
  • i think you are using toolbar view in every fragment view. you must have only one toolbar which should be placed on Activity. you can control the searchview from the activity easily. using many toolbar is bad practices and send search string from activity to fragment use this link : http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android/38741476#38741476 – Noorul Dec 05 '16 at 10:45
  • Before, i use 1 toolbar which is holded by activity. But it turns out that i don't need what i need in fragment 1 (searchview in toolbar, and navigation drawer) in another fragment. So i moved the toolbar and navigation view to fragment. Do you think i'm still need to make 1 toolbar in activity? – zihadrizkyef Dec 05 '16 at 12:49

1 Answers1

0

You can expose the view from the onCreateOptionsMenu() via the fragment and use isIconified() to check the status and setIconified (true) to close it.

FragmentListProduct.java

private SearchView searchView;
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    //...
    searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    //...
}
public SearchView getSearchView() {
    return searchView;
}

MainActivity.java

public void onBackPressed() {
    //...
    SearchView searchView = ((FragmentListProduct)currentFragment).getSearchView();
    if (searchView != null) {
       if (searchView.isIconified()) {
         // the search is extended
         searchView.setIconified(true);
       } else {
         // the search view is closed
       }
    }

/...
}
petrumo
  • 1,116
  • 9
  • 18