如何从搜索结果activity中获取结果?

How to get results from search result activity?

我有一个 Activity (MainActivity),在操作栏中有一个搜索按钮。它按一些输入字符串进行搜索,并在另一个 Activity (SearchResultsActivity) 的 ListView 中显示结果。用户可能会点击任何结果 select 它。

我想 return 由用户编辑的值 select 到主 activity,但它不起作用。我查看了 documentation 但没有找到任何相关内容。 我尝试在结果 activity 中使用 setResult(Intent) 但从未调用主 activity 中的 onActivityResult() 。

我做错了什么?我该怎么做?

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="foo.bar" >

    <!-- ... -->

    <uses-sdk android:minSdkVersion="11" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">

        <!-- Main Activity -->
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main">

            <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchResultsActivity" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Search results activity -->
        <activity android:name=".SearchResultsActivity"
            android:parentActivityName=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
    </application>

</manifest>

Menu.xml:

<menu xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context=".MainActivity">

    <!-- Search -->
    <item android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/action_search"
        android:showAsAction="always"
        android:actionViewClass="android.widget.SearchView" />

    <!-- ... -->
</menu>

主要Activity:

public class MainActivity extends FragmentActivity {
    // ...

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        final MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.activity_main_menu, menu);

        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // never gets called!
        super.onActivityResult(requestCode, resultCode, data);
    }

    // ...
}

搜索结果Activity:

public class SearchResultsActivity extends Activity {
    private ListView listResults;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_results);

        // get the action bar
        ActionBar actionBar = getActionBar();

        // Enabling Back navigation on Action Bar icon
        actionBar.setDisplayHomeAsUpEnabled(true);

        listResults = (ListView) findViewById(R.id.listResults);

        handleIntent(getIntent());
    }

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

    /**
     * Handling intent data
     */
    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);

            final Serializable[] results = find(query);

            listResults.setAdapter(new ArrayAdapter<BusLine>(this, R.layout.list_view, busLines));
            listResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Serializable selectedResult = (Serializable) parent.getItemAtPosition(position);
                    setResult(RESULT_OK, new Intent().putExtra("result", selectedResult));
                    finish();
                }
            });
        }
    }
}

我能理解为什么您可能认为您会使用 setResult 和 onActivityResult 来处理搜索结果中的点击 activity,但事实并非如此。仅当您使用 startActivityForResult 启动 activity 时,这些功能才适用——而不是在显示搜索结果时。

解决方案是以正常方式调用 MainActivity 上的 startActivity,即不使用 activity 结果。像这样:

listResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Serializable selectedResult = (Serializable) parent.getItemAtPosition(position);
        Intent intent = new Intent();
        intent.putExtra("result", selectedResult);
        intent.setClass(SearchResultsActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }
});

现在您还需要在 MainActivity 中捕获此搜索结果,尽管您需要确保处理正常情况和搜索结果情况:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    if (intent.hasExtra("result")) {
        // Launched from search results
        Serializable selectedResult = (Serializable) intent.getSerializableExtra("result");
        ...
    }
    ...
}