How to reset find using android SearchManager

I have one action with a fragment to list some data. I have already implemented a search using SearchView (a beautiful search field in the action bar).

The search works fine, the user enters data, presses the search button, and the results are filtered out as expected.

My problem is that I cannot "reset" search for all results. I already tried installing the Dismiss and Cancel listeners on the searchManager object, but the onDismiss and oncancel methods are never called.

Here is my action on the manifest:

    <activity
        android:name=".ClientesActivity"
        android:label="@string/title_activity_clientes"
        android:parentActivityName=".MenuPrincipalActivity"
        android:launchMode="singleTop" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="MenuPrincipalActivity" />
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/cliente_searchable" />
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
    </activity>

ClientesActivity Methods:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    clientesService = new ClientesService(getBaseContext());
    setContentView(R.layout.activity_clientes);
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, ClienteFragment.newInstance(), ClienteFragment.class.getSimpleName())
                .commit();
    }
    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if(Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        clientesService.findByNameLike(query);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.clientes, menu);
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));
    searchView.setSubmitButtonEnabled(true);
    return true;
}

Listenners, , . o "return true" onCreateOptionsMenu. , , onDismiss, , .

+4
1

android.widget.SearchView#setOnQueryTextFocusChangeListener. , hasFocus , searchview , -/, . , android.widget.SearchView#setOnCloseListener,

, onOptionsItemSelected android.app.Activity # onSearchRequested(), , .


    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
       switch (item.getItemId()) {
         case R.id.search:
            onSearchRequested();//delegate upstream
            break;
         default:
            return super.onOptionsItemSelected(item);
       }
    }

. . , , SearchManager .

0

Source: https://habr.com/ru/post/1543564/


All Articles