marjanbaz
marjanbaz

Reputation: 1042

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 this:

Intent i = new Intent(Game.this, Popup_opis.class);
i.putExtra("brojPoenaPrimljeno", brojPoena);

and in my receiving activity:

Intent mIntent = getIntent(); 
        if(mIntent !=null) {
           int brojPoena = mIntent.getIntExtra("brojPoenaPrimljeno", 0);
        }
tvBrojPoena.setText("You won " + brojPoenaPrimljeno + " points");

Also I tried this:

Intent i = new Intent(getApplicationContext(), Popup_opis.class);
i.putExtra("brojPoenaPrimljeno", brojPoena);

and in my recieving activity:

Bundle extrasPoeni = getIntent().getExtras(); 
        if(extrasPoeni !=null) {
           brojPoenaPrimljeno = extras.getInt("brojPoena");
        }

My receiving activity:

public class Popup_opis extends Activity implements OnClickListener{

    TextView tvOpis,tvNaslov,tvBrojPoena;
    String poslatOpis, primljenOpis;
    int brojPoenaPrimljeno;
    Button OK;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.popup_opis);

        Bundle extras = getIntent().getExtras(); 
        if(extras !=null) {
           primljenOpis = extras.getString("poslatOpis");
        }

        Bundle extrasPoeni = getIntent().getExtras(); 
        if(extrasPoeni !=null) {
           brojPoenaPrimljeno = extras.getInt("brojPoena");
        }

        initVariables();

    }

    private void initVariables() {


        Typeface tv = Typeface.createFromAsset(getAssets(), "ARIALN.TTF");
        OK = (Button) findViewById(R.id.bOK);
        tvOpis = (TextView) findViewById(R.id.tvOpis);
        tvBrojPoena = (TextView) findViewById(R.id.tvBrojPoena);
        tvBrojPoena.setTypeface(tv);
        tvNaslov = (TextView) findViewById(R.id.tvNaslov);
        tvNaslov.setTypeface(tv);
        tvOpis.setTypeface(tv);
        tvOpis.setText(primljenOpis);
        tvBrojPoena.setText("Osvojili ste " + brojPoenaPrimljeno + " poena u ovoj igri.");



    OK.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            finish();

        }
    });
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}

Upvotes: 0

Views: 857

Answers (1)

codeMagic
codeMagic

Reputation: 44571

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

Bundle extras = getIntent().getExtras();

and here

Bundle extrasPoeni = 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
    }
}

Upvotes: 3

Related Questions