MainActivity在QueryTextSubmit上重新启动

时间:2014-02-09 15:26:01

标签: android android-activity android-manifest searchview android-search

我设置了可搜索的配置,但现在我面临着奇怪的问题。当我提交搜索查询时, SearchActivity.class 会启动一秒钟,然后返回 MainActivity.class 。我不知道,会发生什么。

查看我的代码:

主要活动

@Override
        public boolean onCreateOptionsMenu(Menu menu) {

         // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

            MenuItem searchItem = menu.findItem(R.id.action_search);
            mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);

            if (null != mSearchView )
            {
                mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

            }

            SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() 
            {
                public boolean onQueryTextChange(String newText) 
                {


                    return true;
                }

                public boolean onQueryTextSubmit(String query) 
                {
                    String said = query.toString();
                    Log.v(said, "submited");
                    Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
                    intent.setAction(Intent.ACTION_SEARCH);
                    intent.putExtra("query", query);
                    startActivity(intent);

                    return true;

                }
            };
            mSearchView.setOnQueryTextListener(queryTextListener);

            return super.onCreateOptionsMenu(menu);
        }

AndroidManifest

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.animalist.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

           <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

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

        </activity>

        <activity
            android:name="com.example.animalist.MoreActivity"
            android:label="@string/title_activity_more"
            android:launchMode="singleTop"
            android:parentActivityName="com.example.animalist.MainActivity"
            android:screenOrientation="portrait" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.animalist.MainActivity" />
        </activity>

        <activity
            android:name="com.example.animalist.SearchActivity"
            android:label="@string/title_activity_search"
            android:launchMode="singleTop"
            android:parentActivityName="com.example.animalist.MainActivity"
            android:screenOrientation="portrait" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.animalist.MainActivity" />

        </activity>

    </application>

我做错了什么?

1 个答案:

答案 0 :(得分:1)

基于this thread,似乎是Android的错误。

here(日语)找到这个临时“解决方案”。它对我有用。

@Override
public boolean onQueryTextSubmit(String query) {
    mSearchView.clearFocus();

    ......
    // your code to process the query
    ......

    return false;
}