1

I've created a search view using search widget, and when I open and search it works normally, the problem is when I go to any activity, including the searchview activity, and back using backbutton or finish() the activity, the search widget stops working. I mean, I still can type on widget, but the button to search(return) stops working, the searchable activity doesn't open anymore.

In the HomeActivity.java(main) I set up the search widget:

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home_settings, menu);
        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        // Assumes current activity is the searchable activity
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
        // Change Text color from search bar
        final EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
        searchEditText.setImeOptions(DEFAULT_KEYS_SEARCH_LOCAL);
        return true;
    }

in Searchable.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchable);
        Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        if(getSupportActionBar() != null && toolbar != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            toolbar.setTitle("");
        }
.
.
.
 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_search, menu);
        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        // Assumes current activity is the searchable activity
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
        final EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
        searchEditText.setImeOptions(DEFAULT_KEYS_SEARCH_LOCAL);
        return true;
    }
@Override
protected void onNewIntent(Intent intent) {
    setIntent( intent );
    handleSearch( getIntent() );
}

public void handleSearch(Intent intent){
    if( Intent.ACTION_SEARCH.equalsIgnoreCase( intent.getAction() )){
        String query = intent.getStringExtra(SearchManager.QUERY);
        setTitle(query);
        executeSearchShow(query);
    }
}

So as I said, using backButton to back or calling finish() on activity the searchview widget stops working, but if I use homeUpButton it works normally.

I've override onBackPressed in Searchable activity and it works, but if I back from another activity(that I need to finish) to home the widget still stopping to work.

@Override
    public void onBackPressed() {
        onNavigateUp();
    }

AndroidManifest.xml :

    <activity
        android:name=".activities.HomeActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        android:theme="@style/Theme.MySeries"
        android:windowSoftInputMode="adjustPan|adjustNothing">
        <meta-data
            android:name="android.app.default_searchable"
            android:value="br.com.adley.whatsnextseries.activities.Searchable"/>
    </activity>
            <activity android:name=".activities.Searchable"
              android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
                   android:resource="@xml/searchable"/>
    </activity>
Adley
  • 511
  • 1
  • 6
  • 19

0 Answers0