10

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

I am trying to wrap my head around how to do this. Since I am familiar with intents, I found this answer on another post and decided to try it out. Everything looks fine until I get to step 4, when I try to use Intent i = getIntent(); in my second fragment, Studio won't let me use it and says "getIntent(java.lang.String) is deprecated".

This doesn't make sense to me since I have used getIntent() in other programs without issue, and it is letting me use it in my MainActivity (step 2 from the other post) without screaming at me.

I know this can be done without using intents, but I can't figure it out and can't find any really thorough tutorials in order to do so. So I guess my questions are:

  1. Can I make intents work for this purpose still? What should I do to get around this deprecation issue?
  2. Any other advice, explanations, or links to "explain it like I'm 5" tutorials would be very helpful and welcome. I have Googled and read a few, but I am still not understanding this and am becoming increasingly frustrated. It seems like this should be a relatively simple concept.
Community
  • 1
  • 1
rnbee
  • 144
  • 1
  • 1
  • 6
  • Why the downvotes ? This is a perfectly fine question. –  May 31 '15 at 15:49
  • Thank you Werner. There isn't really any info online about this deprecation or what to do about it. As a newbie I find this all especially confusing. – rnbee May 31 '15 at 16:04
  • 4
    `getIntent(java.lang.String)` doesn't seem to match the calling code `i = getIntent();` - am I missing something here? Can we see relevant code snippets? – stkent May 31 '15 at 17:00
  • How do you get the intent? And what type of values are you trying to pass from the intent? It is really strange your problem cause the getIntent method does not get string as parameter. Can you please post your code? – karvoynistas May 31 '15 at 17:13

4 Answers4

10

It is too late for answer but still I am providing my answer for other persons. It is happen because Intent is basically work with an activity. And fragments are not activity, but attached to activity. So simply you need to do this:

Intent intent=getActivity().getIntent();
Keyur Nimavat
  • 3,595
  • 3
  • 13
  • 19
2

Having the same problem while passing Object from an Activity to a Java Class.

Here is what I did

This Activity sends data

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

It recieves data(In Data.class)
Here I tried this but Android Studio says getIntent is deprecated
Intent intent = Intent.getIntent();

So What can I use in place of getIntent(), because all the solutions I find on Internet to this problem, uses getIntent().


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

E_net4
  • 27,810
  • 13
  • 101
  • 139
anuragsinhame
  • 899
  • 9
  • 11
1

On your onCreate

val bundle = intent.extras
    if (bundle != null) {
        idEmployee = bundle?.getString("idEmployee", "")
        idClient = bundle?.getString("idClient", "")
        listAvailable = bundle?.getStringArrayList("listAvailable") as ArrayList<String>

        Log.i("list:", "$listAvailable" )
    }
0

It means, that this method could not be supported in further releases. The method is still in the API for backward compability for an unspecified amount of time. Mostly it is dangerous to use deprecated methods or there is a better way to achieve this. Like it is described here

In this case you should rather use: parseUri(String, int) to achieve this ( according to the android developer api).

Community
  • 1
  • 1
Sebastian Walla
  • 1,104
  • 1
  • 9
  • 23