1

我正在使用带有 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>
4

1 回答 1

0

通过单击 MainActivity_ 中的搜索图标时启动 SearchShowsActivity_ 来修复此问题:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();

    if (itemId == R.id.add_show) {
        SearchShowsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP).start();
        return true;
    }
    return false;
}

在我的 SearchShowsActivity_ 中,我启动了 SearchView:

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        sv = (SearchView) menu.findItem(R.id.search_shows).getActionView();
        sv.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    }

    sv.setFocusable(true);
    sv.setIconified(false);
    sv.requestFocusFromTouch();

    return true;
}
于 2013-09-12T12:17:43.943 回答