Skip to content Skip to sidebar Skip to footer

How To Pass Int Variable To Another Activity?

I'm trying to pass an int value with an intent to another activity, but I always get 0. And it's not 0, I checked. I'm trying to pass value from int variable brojPoena. I tried thi

Solution 1:

Where is this variable brojPoenaPrimljeno? You have this

int brojPoena = mIntent.getIntExtra("brojPoenaPrimljeno", 0);

but are using a different variable name when you call setText()

You are trying to receive the value using the value instead of the key. When you create the Intent

i.putExtra("brojPoenaPrimljeno", brojPoena);  // brojPoenaPrimljeno is the key be trying to use to 

Try

brojPoenaPrimljeno = getIntent().getIntExtra("brojPoenaPrimljeno", 0);

Also, this is minor and not your problem but is inefficient and could cause problems. You are getting the `Intent in two different places. Here

Bundleextras= getIntent().getExtras();

and here

BundleextrasPoeni= getIntent().getExtras(); 

Handling Intent from different Activities

In case this helps, if I have an Activity receiving Intents from multiple places, I will use a String extra to tell the receiving Activity where it came from. For example:

Intent intent = new Intent(SendingActivity.this, ReceivingActivity.class); intent.putExtra("source", "activityName"); // this will be used to know where the intent came from

//receiving activity
Intent recIntent = getIntent();
if (recIntent.getStringExtra("source") != null)
{
     String source = recIntent.getStringExtra("source");
    if (source.equals("activityName"))
    {
         // do stuff
    }
    if (source.equals("differentActivityName"))
    {
         // do other stuff
    }
}

Post a Comment for "How To Pass Int Variable To Another Activity?"