How do I pass extra variables during a search invoked by a SearchView/ Widget?

前端 未结 3 1049
一整个雨季
一整个雨季 2021-02-13 12:55

I am successfully using a search widget in my action bar to perform a search following this guide. The search is fine, but I\'m wondering how to pass additional variables on a s

3条回答
  •  被撕碎了的回忆
    2021-02-13 13:33

    It seems the only way to do this is to intercept new activities created in your activity which is search-enabled. To do this we override the startActivity() method. We can then check to make sure the activity is indeed the search activity, then add an extra to the intent. The working code is below.

    @Override
    public void startActivity(Intent intent) {      
        // check if search intent
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            intent.putExtra("KEY", "VALUE");
        }
    
        super.startActivity(intent);
    }
    

    You can then grab your extra as you would any other extra in your search activity using:

    mValue = intent.getStringExtra("KEY");
    

提交回复
热议问题