Вопрос

I have a strange issue. Only thing when the text in the SearchView is submitted,i just want to do move to another Activity. But what actually happens here,when i submit it, near around 1 to 5 sec it starts the main Activity again. I've tried to put that Intent somewhere else and it works well.

Do you have any idea, how to solve this?

Thanks in advance

part of MainActivity (I use ActionBar tabs btw ...Sherlock)

@Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        super.onPrepareOptionsMenu(menu);

getSupportMenuInflater().inflate(R.menu.main, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        if (null != searchView )
        {
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

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

                return true;
            }

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

                return true;

            }
        };
        searchView.setOnQueryTextListener(queryTextListener);

int currentItem = mPager.getCurrentItem();
            if(currentItem == 0){
                menu.findItem(R.id.action_refresh).setVisible(false);
                menu.findItem(R.id.action_search).setVisible(true);
                menu.findItem(R.id.action_overflow).setVisible(false);
            }
            else if(currentItem == 1) {
                menu.findItem(R.id.action_refresh).setVisible(true);
                menu.findItem(R.id.action_search).setVisible(false);
                menu.findItem(R.id.action_overflow).setVisible(false);
            }
            else if(currentItem == 2) {
                menu.findItem(R.id.action_refresh).setVisible(false);
                menu.findItem(R.id.action_search).setVisible(false);
                menu.findItem(R.id.action_overflow).setVisible(true);
            }
                 return true; }

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.default_searchable"
            android:value=".SearchActivity" />

            <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: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:parentActivityName="com.example.animalist.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.animalist.MainActivity" />
        </activity>
    </application>

</manifest>
Это было полезно?

Решение

Maybe your problem can be that you're querying repeatedly, have you tried to debug and confirm that this is not happening? in case it's happening, you will need to handle which is the right time for starting your activity, you can use a handler post delaying or another solution could be to check if have just come across the onQuerySubmit method and if so, don't start the activity.

Другие советы

In the activity where the SearchView is displayed you need to change

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

to

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

Try this, I think this will work:

searchView.setQuery("", false);
searchView.requestFocus();
return true;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top