How can I handle onBackPressed()
at search mode? I have implemented the search at the ActionBar
, and I want to handle onBackPressed()
.
At MainActivity
I have added this, but it only gets notified when search is closed
@Override
public void onBackPressed() {
mMenu.findItem(R.id.menu_eye).setVisible(true);
mMenu.findItem(R.id.menu_layers).setVisible(true);
super.onBackPressed();
};
and my listener for search action looks like this:
import com.cyrilmottier.polaris.PolarisMapView;
import android.app.SearchManager.OnCancelListener;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SearchView;
public class SearchListener implements SearchView.OnQueryTextListener, SearchView.OnCloseListener, SearchView.OnSuggestionListener, OnClickListener, OnCancelListener{
private Context mContext;
private PolarisMapView mMapView;
private Menu mMenu;
private SearchView mSearchView;
public SearchListener(Context c, PolarisMapView mMapView, Menu mMenu, SearchView mSearchView){
this.setmContext(c);
this.setmMapView(mMapView);
this.setmMenu(mMenu);
this.mSearchView = mSearchView;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
if (Constants.searchPlaceNavigate(query, mContext, mMapView))
return this.onClose();
return false;
}
public Context getmContext() {
return mContext;
}
public void setmContext(Context mContext) {
this.mContext = mContext;
}
public PolarisMapView getmMapView() {
return mMapView;
}
public void setmMapView(PolarisMapView mMapView) {
this.mMapView = mMapView;
}
@Override
public boolean onSuggestionClick(int position) {
String p = mSearchView.getSuggestionsAdapter().getCursor().getString(position);
if(position== 0)
p = mSearchView.getSuggestionsAdapter().getCursor().getString(position*4+1);
if(p != null)
if(position== 0)
Constants.searchPlaceNavigate(mSearchView.getSuggestionsAdapter().getCursor().getString(position*4+1), mContext, mMapView);
else
Constants.searchPlaceNavigate(mSearchView.getSuggestionsAdapter().getCursor().getString(position), mContext, mMapView);
return this.onClose();
}
@Override
public boolean onSuggestionSelect(int position) {
return false;
}
@Override
public boolean onClose() {
mMenu.findItem(R.id.menu_eye).setVisible(true);
mMenu.findItem(R.id.menu_layers).setVisible(true);
mMenu.findItem(R.id.menu_search).collapseActionView();
return true;
}
public Menu getmMenu() {
return mMenu;
}
public void setmMenu(Menu mMenu) {
this.mMenu = mMenu;
}
public SearchView getmSearchView() {
return mSearchView;
}
public void setmSearchView(SearchView mSearchView) {
this.mSearchView = mSearchView;
}
@Override
public void onClick(View v) {
this.onClose();
}
@Override
public void onCancel() {
this.onClose();
}
}
another way is to listen on the MenuItem for the ActionExpand/Collapse :
MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
searchMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do whatever you need
return true; // KEEP IT TO TRUE OR IT DOESN'T OPEN !!
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do whatever you need
return true; // OR FALSE IF YOU DIDN'T WANT IT TO CLOSE!
}
});
SearchView searchView = (SearchView) searchMenuItem.getActionView();
... // Keep doing as you do
I think this way is cleaner, as you listen directly on what you want
Thanks to this thread