I have Develop an application that have search view in action bar and i got issue when i am do search its filter perfectly but when i am press back button its still showing filter data so my question is what is event of back button of Action Bar Search view.?
My Code of Search View is
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(Menus.SEARCH));
searchView.setQueryHint(this.getString(R.string.search));
editSearch = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
editSearch.setHintTextColor(getResources().getColor(R.color.white));
searchView.setOnQueryTextListener(OnQuerySearchView);
private OnQueryTextListener OnQuerySearchView = new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String newText) {
if (TextUtils.isEmpty(newText)) {
listAllContact.clearTextFilter();
} else {
listAllContact.setFilterText(newText.toString());
}
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
String text = editSearch.getText().toString()
.toLowerCase(Locale.getDefault());
adapter.filter(text);
return true;
}
};
Filter Method in Adapter
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
propertyList.clear();
if (charText.length() == 0) {
propertyList.addAll(arrayList);
notifyDataSetChanged();
} else {
for (ContactProperty p : arrayList) {
if (p.getFriendName().toLowerCase(Locale.getDefault())
.contains(charText)) {
propertyList.add(p);
}
}
notifyDataSetChanged();
}
You can add listener for this as :
Try this:
This will collapse the search action item when its focus is lost:
This is the only way I've found to successfully collapse the search item action view when hitting the back button.
In menifest file
For handling back button of Action Bar - Activity declaration part first of all need to define Parent Activity.
Like-
Edit onOptionsItemSelected method thats inside activity
NavUtils.navigateUpFromSameTask(this);
//public static void navigateUpFromSameTask (Activity sourceActivity) //sourceActivity will be finished by this call.
Note: This method should only be used when sourceActivity and the corresponding parent are within the same task.
For More Info :-
http://developer.android.com/reference/android/support/v4/app/NavUtils.html
Like
@Override
You only need to put the "collapseActionView" attribute in the menu layout
That will give you the functionality you look for all by itself.