Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment replacement triggers onQueryTextChange on searchview

This is how I navigate through my app:

  • Open fragment with list
  • Filter list by a text entered in searchview
  • Tap on listitem (list fragment gets replaced by detail fragment)
  • Navigate back (detail fragment gets replaced by list fragment)

When I navigate from list- to detail-fragment, I want to keep the current filter of the searchview in a string variable. I store the value of the searchview when onQueryTextChange is executed.

The problem: I can't store the actual filter-value because onQueryTextChange gets called when I navigate from list to detail because something cleared the text of the searchview.

// ...         searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {         @Override         public boolean onQueryTextSubmit(String s) {             return false;         }          @Override         public boolean onQueryTextChange(String s) {             searchReceived(s);             return true;         }     }); // ...  public void searchReceived(String searchQuery) {     this.stateHolder.searchQuery = searchQuery;     // more code... } 

When I want to restore the filter when navigating back, it just filters with an empty string because the wrong value got stored in this.stateHolder.searchQuery.

Stack:

onQueryTextChange():139, EmployeeListFragment$1 {com.example.exampleapp.fragment} onTextChanged():1153, SearchView {android.widget} access$2000():92, SearchView {android.widget} onTextChanged():1638, SearchView$11 {android.widget} sendOnTextChanged():7408, TextView {android.widget} setText():3816, TextView {android.widget} setText():3671, TextView {android.widget} setText():80, EditText {android.widget} setText():3646, TextView {android.widget} setQuery():511, SearchView {android.widget} onActionViewCollapsed():1250, SearchView {android.widget} collapseItemActionView():1662, ActionBarView$ExpandedActionViewMenuPresenter {com.android.internal.widget} collapseItemActionView():1258, MenuBuilder {com.android.internal.view.menu} clear():521, MenuBuilder {com.android.internal.view.menu} doInvalidatePanelMenu():789, PhoneWindow {com.android.internal.policy.impl} run():221, PhoneWindow$1 {com.android.internal.policy.impl} 

How can I prevent the searchview from being cleared by the system when navigating?

Thanks.

like image 933
Tom Avatar asked May 08 '14 14:05

Tom


1 Answers

After 2 days of Googling i got the solution that will definitely helps you.

I have just cut and pasted code from onCreateOptionMenu() to onPrepareOptionMenu()

I do not know why it happens, I think listener of searchView is not going to be NULL by the way you can change your code this way.

@Override public void onPrepareOptionsMenu(Menu menu) {     super.onPrepareOptionsMenu(menu);     getSherlockActivity().getSupportMenuInflater().inflate(R.menu.menu_all_order, menu);      searchView = (SearchView) menu.findItem(R.id.menu_all_order_search).getActionView();     searchView.setInputType(InputType.TYPE_CLASS_NUMBER);     searchView.setQueryHint("Enter Order No");     searchView.setOnQueryTextListener(this); } 

and Remove:

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

Thanks :)

like image 179
Pratik Butani Avatar answered Sep 23 '22 16:09

Pratik Butani