I have an activity that I normally want to exist in multiple tasks, so that the [Back] button restores a previous state; however, I also want to use a SearchView with an existing activity, without pushing a new one onto the task stack (since I want to search what's currently displayed). Here's my problem:
- If I set the activity's launch mode to "singleTop", the SearchView works with the current activity, but Context.startActivity does not add new activities to the stack.
- If I set the activity's launch mode to "standard", startActivity adds new activities to the stack, but SearchView also creates a new (empty) activity, instead of searching the existing one.
I can't find any way make SearchView use intent flags (e.g. FLAG_ACTIVITY_SINGLE_TOP. I tried going the other way around, setting launch mode to "singleTop" then adding FLAG_ACTIVITY_NEW_TASK to every intent, but that worked only when launching from a different activity class; when I try to launch a new instance of an activity from its own class, Android didn't respect FLAG_ACTIVITY_NEW_TASK and didn't push a new task onto the stack.
Perhaps there's something I can do in Activity.onNewIntent to force the activity to clone itself then.
I'm on the verge of giving up on SearchView and just writing my own custom widget, despite that fact that "Beginning in Android 3.0, using the SearchView widget as an item in the action bar is the preferred way to provide search in your app" (Source) — so far, it seems just too inflexible and non-configurable to be useful in non-trivial cases.
Has anyone found a solution to this problem?
After a lot of reflection and false starts, I realized that SearchView was simply the wrong thing for this task. Google designed SearchView with a very specific type of search activity in mind:
That's not the search model I was using; instead, I wanted to refine a list currently shown on the screen using the query — it was more of a filtering search than a synchronous query and response model. SearchView is not designed for that, so I went ahead and wrote my own widget (and bound it to the search menu item and the search button).
I'm surprised that no one had any comments on this question — usually Android questions generate a lot of discussion. I guess that this was was just too far out there.
I had the same problem as the OP, but have a solution that doesn't mean giving up on using
SearchView
. It's not the perfect solution, but it's fairly clean and seems to work well.As the OP stated...
...so my solution is to go via an intermediary Activity. For this, I created a
DummyActivity
class which basically takes the original intent and forwards it on to your destination activity.So, from my original activity, instead of calling...
...I call...
In my manifest, DesinationActivity is defined as singleTop.
And the only other thing you should need is the DummyActivity class which shouldn't need any major customising...
Hopefully this will work for you, too. Suggestions welcomed. Cheers.