rnbee
rnbee

Reputation: 144

Android: getIntent() is deprecated

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.

Upvotes: 10

Views: 12425

Answers (5)

Gokul Krishnan
Gokul Krishnan

Reputation: 1

Same problem "getIntent(java.lang.String) is deprecated". .

I developed the application and saved as a zip file in android studio electric eel version on a computer (no problem was there in this version). When I opened the project in android studio koala version.

  1. problem1 intent is deprecated
    Intent intent = getIntent();

  2. problem2 need context provided main activity

    recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));

these doesn't worked for me --> Restarted android studio ,clean project , rebuild project.

solution At last, I navigate --> file > invalidate caches selected three options in invalidate caches pop up

application successfully worked for me

Upvotes: 0

anuragsinhame
anuragsinhame

Reputation: 1049

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).

Upvotes: 2

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" )
    }

Upvotes: 1

Keyur Nimavat
Keyur Nimavat

Reputation: 3635

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();

Upvotes: 10

Sebastian Walla
Sebastian Walla

Reputation: 1124

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).

Upvotes: 0

Related Questions