lundi 11 mai 2015

Action Bar not going back to normal after search

I am using onNewIntent() and handleIntent() to use my search query in the activity in which it is searched and not launch a new activity, but after I search, my activity title does not return. Instead there is an icon with a back button, and the search icon next to it, not all the way to the right where it should be. When I press the back button it goes back to how it should be. I have tried using onBackPressed() to simulate this back press, but it closes the original activity as well. I am not sure why this is happening, is my searching activity staying up for some reason?

Here is the related code in my main activity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.add, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView = (SearchView) menu.findItem(R.id.action_search)
            .getActionView();
    ComponentName cn = new ComponentName(this, SearchActivity.class);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
    searchView.setIconifiedByDefault(false);
    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean queryTextFocused) {
            if(!queryTextFocused) {
                searchView.onActionViewCollapsed();
                searchView.setQuery("", false);
            }
        }
    });
    return true;
}

@Override
public void startActivity(Intent intent) {
    // check if search intent
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        onNewIntent(intent);
    } else {
        super.startActivity(intent);
    }

}

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

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        search(query);//this is my own method, performs search opeations
        searchView.onActionViewCollapsed();
    }
}

Aucun commentaire:

Enregistrer un commentaire