Skip to content Skip to sidebar Skip to footer

Reset The Searchview On Fragment Change In Viewpager With Actionbar Tabs

I have implemented two fragments with ActionBar tabs. I am using Sherlock fragments. When I start a fragment I create a searchview on the action bar. The problem is: When I search

Solution 1:

For those who are still scratching their heads and are not able to find a solution, here it how you do it. It is pretty simple:

Just assign an ID to your searchviews when you are creating it (by creating the ids.xml and reserving the id there and assigning it on runtime). Now in onTabUnselected() method of the main tab activity, identify the searchviews and use function setIconified(true) twice (one to remove the text and one to collapse the searchview).

Do not forget to add a check for null query string in onQueryTextChange method of your searchview, it created a problem for me.

Solution 2:

Android gives you methods like onCreate and onDestroy to work with activity and also for the menus onCreateOptionsMenu and onDestoryOptionsMenu. You can use onDestroyOptionsMenu method to clear your SearchView or to realize your another ideas.

for example:

@Override
    public void onDestroyOptionsMenu() {
       super.onDestroyOptionsMenu();

       if (searchView != null && 
          !searchView.getQuery().toString().isEmpty()) {

          searchView.setIconified(true);
          searchView.setIconified(true);
       }
    }

It works for me, good Luck.

Post a Comment for "Reset The Searchview On Fragment Change In Viewpager With Actionbar Tabs"