SearchView and BackPress event handling

509 views Asked by At

I have include SearchView in the HomeScreen . I use SearchView for place search. This is my screenshot of HomeScreen: enter image description here

This is my code in onCreateOptionsMenu method:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.home, menu);
    searchItem = menu.findItem(R.id.menu_item_search_places);
    searchItem.setIcon(FontIconDrawable.inflate(getContext(), R.xml.ic_search));
    searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setOnSuggestionListener(this);
    searchView.setOnQueryTextListener(this);
    placesSearchAdapter = new PlacesSearchAdapter(this.getContext());
    searchView.setSuggestionsAdapter(placesSearchAdapter);
    searchView.setMaxWidth(750);
    MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            corLDriveInfoContainer.setVisibility(View.GONE);
            fabDrivingDirection.setVisibility(View.VISIBLE);
            mapView.deleteAllAnnotationsAndCustomPOIs();
            return true;
        }
    });
}

This is my SearchView when I click on SearchIcon. enter image description here

This is my SearchView result:

C.enter image description here

I have FabButton to drive the selected position.

D.enter image description here

But, my problem is, How do go back to View C from View D when I press back button?

2

There are 2 answers

0
Nilesh Singh On

You should override onBackPressed method in your activity like this

@Override
public void onBackPressed() {
   super.onBackPressed();
   //change your View from D to C using the same code you used to go to D from C
}
0
Satan Pandeya On

I include this to solve my problem in onBackPress method:

@Override
public void onBackPressed() {
     searchItem.expandActionView();
     searchView.setQuery("", false);
     searchView.clearFocus();

  super.onBackPressed();
}