Resetting Search Widget (SearchView) value

有些话、适合烂在心里 提交于 2019-12-04 10:00:42

问题


I've got 2 activites : the first, HomepageActiviy, have a search widget that search data using another activity, SearchActivity.

What I want to do is when I go back from SearchActiviy to HomepageActivity, the search widget go collapsed and with a empty text.

I've tried to do this following thing :

public class HomepageActivity extends Activity {
    @TargetApi(11)
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.projectslist, menu);

        if(Build.VERSION.SDK_INT >= 11) {
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView = (SearchView) menu.findItem(R.id.homepage_search).getActionView();
            ComponentName component = new ComponentName(this, SearchActivity.class);
            searchView.setSearchableInfo(searchManager.getSearchableInfo(component));
            searchView.setIconifiedByDefault(true);
            searchView.setQuery("", false);

        }

        return super.onCreateOptionsMenu(menu);
    }

    […]

    @TargetApi(11)
    @Override
    protected void onRestart() {
        super.onRestart();
        if(Build.VERSION.SDK_INT >= 11)
            invalidateOptionsMenu();
        launchAsynchronousImageDownload();
    }
}

If the widget is well displayed as collapsed, the text in the widget still remember searched text (after I re-open the widget). How can I reset the text of the widget?

Thanks for any help! ;)


回答1:


You might also try the following:

searchView.setQuery("", false);
searchView.clearFocus();



回答2:


this is the magic

searchView.setQuery("",false); //clear the text

searchView.setIconified(true);//close the search editor and make search icon again

in the HomepageActivity insert the onSaveInstanceState function after oncreate function

this function will trigger everytime you open a new activity , before opening new activity it will reset the value of Search Widget

  protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
         searchView.setQuery("", false);
        searchView.setIconified(true);
    }



回答3:


This worked for me:

First, I declared the menu item variable at the top of the activity:

private MenuItem mSearchMenuItem;

I defined the variable in OnCreateOptionsMenu():

mSearchMenuItem = menu.findItem(R.id.action_search);

I declared invalidateOptionsMenu() in onResume():

@Override
protected void onResume() {
    invalidateOptionsMenu();
    super.onResume();
}

Lastly, I called collapseActionView() on the menu item in onPrepareOptionsMenu().

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    mSearchMenuItem.collapseActionView();

    return super.onPrepareOptionsMenu(menu);
}



回答4:


searchView.setQuery("", false);
searchView.setIconified(false);



回答5:


I had this problem too and it worked if I put it in onPrepareOptionsMenu.

@Override
public boolean onPrepareOptionsMenu (Menu menu) {
     SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
     searchView.setQuery("", false);
     // rest of code...
}



回答6:


Just found an ugly way to make it work (read comments to see differences):

public class HomepageActivity extends Activity {
    // Declaring SearchView as an instance object
    private SearchView searchView;

    @TargetApi(11)
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.projectslist, menu);

        if(Build.VERSION.SDK_INT >= 11) {
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            // Using instance var instead of local var
            searchView = (SearchView) menu.findItem(R.id.homepage_search).getActionView();
            ComponentName component = new ComponentName(this, SearchActivity.class);
            searchView.setSearchableInfo(searchManager.getSearchableInfo(component));
            searchView.setIconifiedByDefault(true);
            // Setting query is not anymore required
            //searchView.setQuery("", false);
        }

        return super.onCreateOptionsMenu(menu);
    }

    […]

    @TargetApi(11)
    @Override
    protected void onRestart() {
        super.onRestart();
        // Do not need to recreate menu
        /*if(Build.VERSION.SDK_INT >= 11)
            invalidateOptionsMenu();*/
        if(Build.VERSION.SDK_INT >= 11) {
            // Calling twice: first empty text field, second iconify the view
            searchView.setIconified(true);
            searchView.setIconified(true);
        }
        launchAsynchronousImageDownload();
    }
}

It's pretty ugly, I think, so if anybody as a better idea, just tell me :)




回答7:


Kotlin

Here's a cleaner solution, it fixes the following:

  1. Issue of showing filtered list after screen rotation.
  2. Issue of showing filtered list when the user switches to other app and returns back.
  3. Issue of search menu shifting to the left when the user returns back.

No need to iconify if you are invalidating, it's done automatically.


override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    searchView.setQuery("", false)
    (activity as YourActivity).invalidateOptionsMenu()
}

Make sure to change YourActivity to the Activity name in which you have your Fragment.



来源:https://stackoverflow.com/questions/13142678/resetting-search-widget-searchview-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!