I have problem with Activities navigation, searching works good.
Activities hierarchy looks like that:
/ MyListActivityA -- ItemActivityA
MainActivity -- MyTabActivity -- MyListActivityB -- ItemActivityB
\ MyListActivityB -- ItemActivityC
Tabs in TabActivity are created using Intents with MyListActivity.
MyListActivities are declared in manifest like below:
<activity
android:name=".views.OrderListView">
<intent-filter>
<action
android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable_orders" />
</activity>
Every MyListActivity has own SearchRecentSuggestionsProvider.
First resolved problem
When invoked search on any of MyListActivity I got the activity outside the MyTabActivity. Therefore implemented redirecting to MyTabActivity.
In onCreate() of MyListActivity intent action is checked. If it'sIntent.ACTION_SEARCH
then start TabActivity and finish current, like below:
if(Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.ge开发者_如何学GotStringExtra(SearchManager.QUERY);
intent.setClass(context, MyTabActivity.class);
intent.setAction(MyTabActivity.ACTION_SEARCH_PROXY);
Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
if(appData != null) {
intent.putExtras(appData);
}
intent.putExtra(Constants.ACTIVITY_TYPE, activityType);
intent.putExtra(ActivityTabView.EXTRA_SEARCH_QUERY, query);
context.startActivity(intent);
context.finish();
}
When MyListActivity find
ActivityTabView.EXTRA_SEARCH_QUERY
it makes search query on list. And problem resolved.
Second resolved problem
Clicking "Back" button clears search query - that's ok. But clicking "Back" again shows past searches.
That's why I put noHistory to MyTabActivity:
<activity
android:name=".views.MyTabActivity"
android:noHistory="true">
</activity>
Third UNresolved problem
Now, for example, going from MyListActivityA to ItemActivityA and clicking "Back" redirects to MainActivity. I can't do back to MyTabActivity because of noHistory parameter.
Is there any good solution for using Android searching in TabActivity for each activity in tab respectively?
I recommend to read Tasks and Back Stack. It gave me better understandings of activities stack in android.
My solution:
Invoked Quick Search Dialog starts the activity by which it was invoked, in this example it is MyListActivity. That makes TabActivity dissapear, that's why there is no visible TabWidget. Therefore in MyListActivity is implemented redirection to MyTabActivity (see First resolved problem). MyTabActivity just redirects extras, with search query, included in intent to specific MyListActivity.
All you have to do now is add one line. Set FLAG_ACTIVITY_CLEAR_TOP flag on intent that starts MyTabActivity.
if(Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
intent.setClass(context, MyTabActivity.class);
intent.setAction(MyTabActivity.ACTION_SEARCH_PROXY);
Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
if(appData != null) {
intent.putExtras(appData);
}
intent.putExtra(Constants.ACTIVITY_TYPE, activityType);
intent.putExtra(ActivityTabView.EXTRA_SEARCH_QUERY, query);
//added line
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
context.finish();
}
Then normal activities navigation is preserved.
精彩评论