Monday, July 14, 2014

Android: Return search query to current activity

Android: Return search query to current activity

In your Application Manifest you need to define the current activity as a searchable activity.
 android:name="BrowseItems" android:label="@string/browseitems"
            android:launchMode="singleTop">
            
                 android:name="android.intent.action.SEARCH" />
            
android:name="android.app.searchable" android:resource="@xml/itemsearchable" /> You then use the following code, which is from http://developer.android.com/guide/topics/search/search-dialog.html#LifeCycle
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);
    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
      String query = intent.getStringExtra(SearchManager.QUERY);
      // Do work using string
    }
}
You can then use the string to reload your activity, if its a list activity you can call your code that you use to load data and use the string in that.

No comments:

Post a Comment