我正在使用带有 SherlockActionBar 的 AndroidAnnotations,并尝试实现从 MainActivity 到 SecondActivity 的搜索。在 SecondActivity 我希望能够再次搜索。但是,如果我单击 SecondActivity 中的搜索小部件,它会返回到第一个活动。如果没有 SecondActivity 中的搜索小部件,它可以正常工作。
先感谢您!
主要活动:
@EActivity
@OptionsMenu(R.menu.main)
public class MainActivity extends TabSwipeActivity {
SearchView sv;
@SystemService
SearchManager searchManager;
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
sv = (SearchView) menu.findItem(R.id.search_shows).getActionView();
sv.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
}
搜索节目活动:
@EActivity(R.layout.activity_search_shows)
@OptionsMenu(R.menu.search_menu)
public class SearchShowsActivity extends SherlockActivity {
SearchView sv;
@SystemService
SearchManager searchManager;
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
sv = (SearchView) menu.findItem(R.id.search_shows).getActionView();
sv.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
query = intent.getStringExtra(SearchManager.QUERY);
// perform search
}
}
main.xml(菜单)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/search_shows"
android:title="@string/search_label"
android:icon="@android:drawable/ic_menu_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="com.actionbarsherlock.widget.SearchView" />
AndroidManifest.xml
<meta-data
android:name="android.app.default_searchable"
android:value=".activities.SearchShowsActivity_" />
<activity
android:label="@string/app_name"
android:name=".activities.MainActivity_">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:configChanges="keyboardHidden|orientation|screenSize"
android:launchMode="singleTop"
android:name=".activities.SearchShowsActivity_">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>