1

我正在关注这个相对简单的教程,了解如何手动启动 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);

}
4

1 回答 1

2

首先在 manifest.xml 中制作 Activity(A) singleInstance/singleTopandroid:launchMode="singleInstance|singleTop"
然后使用onNewIntent(Intent intent)from Activity(A) 来处理 Intent(intent)

编辑:
ActivityA.java

@Override
protected void onNewIntent(Intent intent) {
    handleIntent(intent);
}

请阅读本教程以全面了解正在发生的事情

于 2013-07-17T22:25:14.027 回答