SearchView launches searchable activity without intent

╄→гoц情女王★ 提交于 2019-12-10 21:52:14

问题


I am using a SearchView for starting a new activity that shows the search results. I have followed the following sources:

  • Android tutorial: Creating a Search Interface
  • SO: Start new activity from SearchView
  • SO: Cannot get searchview in actionbar to work

The new searchable activity ListActivity is launched from a SearchView widget inside the App Bar in MainActivity. The new searchable activity is started but the search intent is missing (onNewIntent method is never called).

Searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_label"
    android:hint="@string/search_hint" >
</searchable>

AndroidManifest.xml

<application
    ...
    <meta-data
        android:name="android.app.default_searchable"
        android:value=".ui.ListActivity" />
    <activity
        android:name=".ui.MainActivity"
        ...
    </activity>
    <activity
        android:name=".ui.ListActivity"
        android:launchMode="singleTop"
        ...
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
    </activity>
</application>

MainActivity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // setSupportActionBar
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);    
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false); 
        return true;
    }
}

ListActivity

public class ListActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {    
        Log.d(TAG, "onCreate invoked");    //Log Printed
        ...
    }

    @Override
    protected void onNewIntent(Intent intent) {
        Log.d(TAG, "onNewIntent invoked");    //Log NOT Printed
    }
}

Consider that I have also replaced getComponentName() with new ComponentName(this, ListActivity.class) but got the same result: no errors, no intent query.


回答1:


As per onNewIntent()'s documentation:

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

singleTop only applies if the activity (in your case the ListActivity) is on top - instead of creating a new instance, it would reuse the existing one. However, if you are only searching from MainActivity (hitting back on ListActivity after a search), then you are destroying the ListActivity instance and then creating a new instance - leading to onCreate() being called, but not onNewIntent().




回答2:


The onNewIntent method is not called for the "first" Intent an activity receives. Have a look at the documentation http://developer.android.com/intl/es/reference/android/app/Activity.html#onNewIntent(android.content.Intent).

In your case you can just call getIntent in onCreate to get your search query:

@Override
public void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
    }
}


来源:https://stackoverflow.com/questions/35346074/searchview-launches-searchable-activity-without-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!