我正在关注这个相对简单的教程,了解如何手动启动 aSearchableActivity
以便我可以从中获得result
回报。
他基本上是将基础 Activity
(活动A)设置为可搜索活动(在清单中),以便它接收搜索意图,然后手动启动真正的搜索Activity
(活动B)startActivityForResult
,
更改清单后,他只有一个代码块。我的问题:
你把这段代码放在哪里?
private void handleIntent(Intent intent) {
// Get the intent, verify the action and get the query
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
// manually launch the real search activity
final Intent searchIntent = new Intent(getApplicationContext(),
MySearchActivity.class);
// add query to the Intent Extras
searchIntent.putExtra(SearchManager.QUERY, query);
startActivityForResult(searchIntent, ACTIVITY_REQUEST_CODE);
}
Activity
甲还是乙?你怎么称呼它?
额外信息 - 我目前是如何做的 - 这会被删掉吗?
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setupNewSearchView(searchItem, searchManager);
}
return true;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupNewSearchView(final MenuItem searchItem, SearchManager searchManager) {
android.widget.SearchView searchView = (android.widget.SearchView) searchItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
}