3

I've set the searchable config but now am I facing strange problem. While I submit the search query the SearchActivity.class is launched for a second and then goes back to MainActivity.class. I don't know, what happens.

See my codes:

in MainActivity:

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

What am I doing wrong?

marson
  • 913
  • 10
  • 32

1 Answers1

1

Based on this thread, it seems that it is a bug of Android.

Found this temporary "solution" from here (Japanese). It works for me.

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

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

    return false;
}
Feng Guo
  • 11
  • 1
  • 3