Hello everyone I am new to android and I am trying to pass an integer from activity A to B and I do it in the following way:
Intent intent = new Intent(this, newClass.class);
intent.putExtra("entero1", 5);
startActivity(intent);
And in activity B I try to receive the value 5 in this way:
Intent intent = getIntent();
int activityValue = intent.getIntExtra("entero1", 0);
By doing this I always return the default value, in this case the 0 and what I want is to receive the 5 that was put in the putExtra
.
Can someone explain to me why this happens?
UPDATE:
I have found how to make the getIntExtra return the desired value.
Here's my example:
First in the activity class "A" I declare a global variable
public static final String rack_number = "com.engeimanga.smartrack.racknumber";
(The value of this string is given by: (your domain). (the name of your app). (the name you want)
When I create the intent and I do the putExtra I send the name of the global variable and the value I want to send to activity "B"
Intent intent = new Intent(this, newClass.class);
intent.putExtra(rack_number, 5);
startActivity(intent);
Within activity "B" to receive the value correctly I do the following:
Intent intent = getIntent();
int number;
number = intent.getIntExtra(LoginScreen.rack_number, 0);
Doing this in the variable number is already saved the value that was sent from activity "A" The first argument of getIntExtra is: (the name of the activity from which they send the extra). (the name of the variable global that we created).
All this I did based on an example of the android developers page.
To be honest I do not understand very well why it works in this way and not as I had previously if someone could explain it to me I would appreciate it. Meanwhile here is a way to do it.
Greetings