关于如何手动启动SearchableActivity
,我正在关注relatively simple tutorial,以便我可以从中获取result
。
他基本上将 base Activity
(活动A)设置为可搜索的活动(在清单中),以便它接收搜索意图,然后手动启动真实搜索{ {1}}(活动B)Activity
,
更改清单后,他只有一个代码块。我的问题:
你把这段代码放在哪里?
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);
}
A还是B?你在哪里称呼它?
额外信息 - 我目前的表现如何 - 是否会被删除?
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;
答案 0 :(得分:2)
首先在manifest.xml android:launchMode="singleInstance|singleTop"
中制作Activity(A)singleInstance / singleTop
然后使用Activity(A)中的onNewIntent(Intent intent)
来处理内容(意图)
编辑:
ActivityA.java
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
请阅读此tutorial以全面了解所发生的事情