Reset SearchView when changing a fragment in an ActionPar tabbed ViewPager

I implemented two tabs with ActionBar tabs. I use Sherlock fragments. When I start the fragment, I create a searchview in the action bar.

The problem is this: when I searched for something on the first fragment and did not close the search or deleted the text, I move on to the next fragment, I get a minimized searchview, and that's fine. But when I go back to my first fragment again, I see a list sorted as before, but the search crashes. The only way to return to the original list is to click the search query once and close it.

How do I reset the search for the first fragment when I go to the second fragment, or how can I save the opening of the search when I come to the first fragment?

Any of the solutions will be executed.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
 >

<item android:id="@+id/action_search"
    android:title="search"
    android:icon="@drawable/search"
    android:orderInCategory="100"
    android:showAsAction="always|collapseActionView"
    android:actionViewClass="com.actionbarsherlock.widget.SearchView" />

</menu>

   @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
        {
        super.onCreateOptionsMenu(menu, inflater);

        inflater.inflate(R.menu.main, menu);
        SearchView sv = (SearchView)getActivity().findViewById(R.id.action_search);
          sv.setOnQueryTextListener(new OnQueryTextListener() {
              @Override
              public boolean onQueryTextSubmit(String query) {
                  //...
                  return false;
              }
              @Override
              public boolean onQueryTextChange(String newText) {
                  bindingData.resetData();


                  bindingData.getFilter().filter(newText.toString());
                  return false;
              }
          });
    }
+4
2

, , . :

( ids.xml ). onTabUnselected() setIconified (true) ( , ).

onQueryTextChange , .

+3

Android onCreate onDestroy , onCreateOptionsMenu onDestoryOptionsMenu. onDestroyOptionsMenu, SearchView .

:

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

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

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

, .

+2

All Articles