Android: getIntent () is deprecated

My program consists of MainActivity and two fragments. I need one chunk to take the String value from the user and pass it to the second chunk.

I'm trying to get my head around how to do this. Since I'm familiar with intent, I found this answer on another post and decided to try it. Everything looks fine until I get to step 4, when I try to use the Intent i = getIntent();

in the second snippet, Studio won't let me use it and says "getIntent(java.lang.String) is deprecated"

.

It doesn't make sense to me since I've used it getIntent()

in other programs without issue and it allows me to use it in mine MainActivity

(step 2 from another post) without yelling at me.

I know it can be done without using intents, but I cannot figure it out and cannot find any really thorough tutorials for this. So I think my questions are:

  • Can I make intentions for this purpose? What should I do to work around this deprecated issue?
  • Any other advice, explanation or links to "explain how I am - 5" would be very helpful and welcome. I have Googled and read a few, but I still don't get it and get more frustrated. It looks like it should be a relatively simple concept.
+3


source to share


3 answers


Too late for an answer, but still I am providing my answer to other people. This is because the Intent mainly deals with the activity. And fragments are not activity, but attachment to activity. So simple you need to do this:



Intent intent=getActivity().getIntent();

      

+3


source


Having the same problem when passing an object from Activity to Java class. I see no reason to give this Question downVote. This is what I did

This operation sends data

Salaray newSalary = new Salary();
Intent intent = new Intent(ViewData.this,Data.class);
intent.putExtra("SalaryObj", newSalary);

      




Gets data (in Data.class)
Here I tried it, but Android Studio says getIntent is deprecated
Intent intent = Intent.getIntent();

So, what can I use instead getIntent()

, because all the solutions I have found on the internet for this problem use getIntent()

.



EDIT:
I was playing around with this and found myself trying to get data in Java Class (Not Activity). But when I used it in Activity, it works fine. But it comes to me another question, how to send data from an Activity class to Java (which is not an Activity).

+1


source


This means that this method may not be supported in future releases. This method is still in the API for backward compatibility indefinitely. It is mostly dangerous to use legacy methods, or is there a better way to achieve it. As described here

In this case, you should use: parseUri(String, int)

to achieve this (according to android developer api ).

0


source







All Articles