getIntent () deprecated, use parseUri () and toUri () correctly

0

The Intent class documentation says that its method getIntent () is deprecated and instead it is recommended to use parseUri() . In turn, in the section of parseUri() it is said that the URI (its parameter) can be returned from toUri() .

So, what I've been trying is to start a second activity. In the second activity I try to recover the intent with parseUri() passing as a parameter the String I got from toUri() in the activity (seen by console), but I can not get it to work.

Someone can show me the correct way to use these methods. I mean, for example, assign the URI returned by toUri() to a static public constant that can then be accessed from another class to retrieve the intent using parseUri() .

If the question was not understood, please see this example . Ctrl + F to find getIntent () (only appears once). That is what I want to achieve. But instead of getIntent() , use parseUri() and toUri() .

    
asked by felipe 19.04.2018 в 04:39
source

2 answers

2

The method to obtain the data of Intent that initiated an activity is not other than the method getIntent of the class Activity , which is not obsolete .

From the Android documentation :

  

getIntent

Intent getIntent ()
     

Return the intent that started this activity.

As you can see, there is no news that this method is deprecated.

The method that has been declared obsolete is getIntent of class Intent , as you indicate in your question.

For more security, the official example that Android shows to get the data of Intent that started an activity is based on getIntent() :

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

    // Get the Intent that started this activity and extract the string
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Capture the layout's TextView and set the string as its text
    TextView textView = findViewById(R.id.textView);
    textView.setText(message);
}

At no time does Android say that, in this case, we have to resort to parseUri . Also, parseUri does not get a Intent but instead creates a new one , as the method definition says :

  

parseUri

Intent parseUri (String uri, 
                int flags)
     

Create an integer from a URI. This URI may encode the action, category, and other intent fields, if it was returned by toUri (int).   If the Intent was not generated by toUri (), its data will be the entire   URI and its action will be ACTION_VIEW.

I hope that with this your doubt is clarified.

    
answered by 19.04.2018 / 21:01
source
0

If you are looking to send some variables (parameters) through an Intent, I recommend you use:

Intent i= new Intent();
i.putExtra("nombreCampo", "contenido");

And then to get it:

String id = intent.getStringExtra("nombreCampo"); //Dentro estará "contenido"

I hope it helps you.

    
answered by 19.04.2018 в 14:54