getIntentExtra returns default value

1

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

    
asked by Kuii Maldonado 11.03.2018 в 18:36
source

2 answers

1

It is always good to verify the non-nullity of the past data in Intent for various reasons:

  • because a null value may be happening for any reason beyond our control
  • because we can have Activitys that in some scenarios need extra data, but in others not
  • etc

To handle any of these possible situations, you can use a ternary operator to verify if a null value is not being passed in Intent . In this way, the ternary operator will assign to the variable either the value passed in Intent , or a default value ( 0 in this case).

To do this, in the onCreate of the Activity that will receive the data, you can put this:

int activityValue = (this.getIntent().getExtras() != null) ? getIntent().getIntExtra("entero1") : 0;.

The value of activityValue will have: or the number that is passed in Intent or 0 in case no value has been passed.

    
answered by 11.03.2018 / 20:48
source
0

You can validate first if you really receive a bundle with information:

   Bundle extras = getIntent().getExtras();
    if (extras != null) {
      //Contiene información.
    }

If the bundle contains information, then verify that the values you want to obtain actually exist:

   int number;
   Bundle extras = getIntent().getExtras();
    if (extras != null) {
      //Contiene información.

        if (extras.containsKey("entero1")) {
            number = extras.getInt("entero1");
        }

    }
    
answered by 12.03.2018 в 16:11