I am using a SearchView for starting a new activity that shows the search results. I have followed the following sources:
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.
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()
.