Android: Return search query to current activity

前端 未结 9 1127
半阙折子戏
半阙折子戏 2020-12-07 16:34

I followed the steps described on http://developer.android.com/guide/topics/search/search-dialog.html to implement a search feature in my notepad application.

My pro

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 17:15

    In your Application Manifest you need to define the current activity as a searchable activity.

    
                
                    
                
                
    
    

    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.

提交回复
热议问题