1

I want to pass a variable through a button from one class to another using intents.

This is the code I have so far:

Class A:

     val i = Intent (this, ClassB::class.java)
        intent.putExtra("IS_BLUE", 1)
        startActivity(i)

to class B:

val isBlue: Long = intent . getLongExtra("IS_BLUE", 0)

I am trying to write an if statement to check if it equals 1 or 0. Since the default value is 0 it keeps taking that as the variable instead of what I put it to be in the previous class and going for the else statement.

 if (isBlue.equals(1)) { ... } else { ... }

I might have gotten my types wrong but when I tried with

intent.getIntExtra("IS_BLUE", 0)

I was getting the same issues and some errors in my if statement as well. I am getting no errors and I can run my app fine, I just can't get the variable to pass. Please help.

  • Let the compiler do the heavy work. For instance,`isBlue: Long`, that can be derived by the compiler. Use `isBlue == 1` or better, use `intent.putExtra("IS_BLUE", true)` and use `if (isBlue)` – Michiel Dec 04 '19 at 20:21
  • I get a type mismatch because it's now asking for a Boolean but I specified long. Also when I change it to Boolean, it keeps returning the fefault value still. –  Dec 04 '19 at 20:27
  • Could you post your code? – Michiel Dec 04 '19 at 20:33
  • I am doing a project for UNI so I don't want to get any misconduct going on. This is all the relevant code I can post I think. –  Dec 04 '19 at 20:48
  • Your question says that you're checking for the value in "class B", but your `Intent` seems to be directing you to `ClassA`... is this a typo? – Ben P. Dec 04 '19 at 21:18
  • Where is your `val isBlue = intent.get...` code located? Inside `onCreate()`? – Ben P. Dec 04 '19 at 21:56
  • It is inside onMapReady() which is inside my main class along with onCreate() –  Dec 05 '19 at 16:19

2 Answers2

1

You are comparing between two convertible types which gives wrong output.

Try using

val isBlue = intent.getIntExtra("IS_BLUE", 0)

if (isBlue == 1) { ... } else { ... }

Instead of

if (isBlue.equals(1)) { ... } else { ... }
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
  • I had that previously and had to change the getLongExtra to getIntExtra because the if statement wouldn't compile otherwise, but same issue. –  Dec 04 '19 at 20:23
  • If you want to use Long, then you have to put `L` beside 1 like `1L` – Md. Asaduzzaman Dec 04 '19 at 20:25
  • It keeps returning the default value still. –  Dec 04 '19 at 20:30
  • Probably you call this `val isBlue: Long = intent . getLongExtra("IS_BLUE", 0)` outside `onCreate`. Am I right? – Md. Asaduzzaman Dec 04 '19 at 20:35
  • Actually I have a map activity that opens up when a button is clicked. I want the isBlue to determine the color of markers that pop up on the map. I was creating it inside a method called onMapReady and it didn't crash my code. But when I tried to create it outside of the method it would load the app but crash as soon as I clicked the button. –  Dec 04 '19 at 20:48
0

Instead of using intent.getIntExtra() try:

intent.extras?.getInt("IS_BLUE",0) 

or intent.extras?.getLong("IS_BLUE") or intent.extras?.get("IS_BLUE") depending on the dataType you used to pass the data.

because_im_batman
  • 975
  • 10
  • 26
  • It just uses null as the value now. If I try and put the getLongExtra line anywhere else but the actual method, my app crashes. But if it's in the method then it points to null. –  Dec 04 '19 at 21:28
  • you mean the value of the expression : intent.extras?.getInt("IS_BLUE",0) is null? then you are not passing the arguments correctly. Check the examples here : https://stackoverflow.com/questions/51181747/passing-arguments-to-intent-android – because_im_batman Dec 04 '19 at 21:32
  • also please check this line of code: val i = Intent (this, ClassA::class.java) you say your secondActivity is ClassB and you are putting the data in Class A. right? Then, the correct intent should be sth like, val i = Intent (this, ClassB::class.java) – because_im_batman Dec 04 '19 at 21:35