What to use in place of .getIntent

1

Does anyone know what syntax I use in place of .getIntent. Here is my code:

 Intent myIntent = Intent.getIntent();
    if(Intent.ACTION_SEARCH.equals(myIntent.getAction())){
        String query = myIntent.getStringExtra(SearchManager.QUERY);
        Toast.makeText(this, "", Toast.LENGTH_SHORT).show();

When I put Intent.getIntent () it does not compile because this method is already deprecated.

    
asked by anonymous 22.06.2016 / 13:12

1 answer

2

Hello, Arthur.

That's not how it's done. From what I see in your code, you're wanting to use an attempt received from a search to extract the text you typed in the search. The issue is that you are not getting the instance of the intent received by the search by doing Intent.getIntent (URI), this method is not suitable for this. Your myIntent does not contain the query.

By default, when a user performs an in-app search, a new instance of your activity is created and executed. You can catch this intent by calling the getIntent() method in one of the Activity lifecycle methods, for example, within the onCreate() method.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_funcionariostatus);

    Intent myIntent = getIntent();
}

You can make an interception of the search intent before a new instance of your Activity is created and executed (I do not indicate this interception because opening an instance for each search the user performs is interesting to keep a history and give the possibility of returning the previous search just by going back on the previous screens). This can happen in the Activity startActivity(Intent intent) method that is called every time you startActivity(intent) , which by the way is done automatically when the user performs the search. The attempt received in this method is the same as the one you would capture in onCreate() doing getIntent() , because it is transferred to activity to be instantiated. So, you can do the verification to confirm it is the search intent and then do what you want. You will prevent a new activity from being opened by preventing the parent method from being called super.startActivity(intent); , since it will initiate the process of instantiating the new activity. This way:

@Override
    public void startActivity(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            //Do something with the query...
        } else {
            super.startActivity(intent);
        }
    }
    
22.06.2016 / 13:54