ActionBarSherlock搜索小部件从SecondActivity返回FirstActivity

时间:2013-08-08 20:35:18

标签: android actionbarsherlock android-annotations

我正在使用AndroidAnnotations与SherlockActionBar,并试图实现从MainActivity到SecondActivity的搜索。在SecondActivity中,我希望能够再次搜索。但是,如果我单击SecondActivity中的搜索小部件,它将返回到第一个活动。没有SecondActivity中的搜索小部件,它工作正常。

提前谢谢!

MainActivity:

@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;
    }
}

SearchShowsActivity:

@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>

1 个答案:

答案 0 :(得分: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;
}