0

I need some help with using integer from one activity to another.

I am making some basic math program(game). It gets two random numbers, random operator, and 30 secs to solve math problems as much as you can.

If you solve problem u get 1 point.

Anyway right now, I want to get number of points that user have made and use it in another activity called *RankActivity*.

Main activity is called *BrzoRacunanjeActivity* and it contains button and one *int* called *poenibrojanje* which get number of points that user have made, and when I click on button, it opens new Activity with this line:

startActivity(new Intent(this, RankActivity.class));

As you can see another Activity is called RankActivity, and there I wrote :

*BrzoRacunanjeActivity a1 = new BrzoRacunanjeActivity();*

*System.out.println("Number of points:" + a1.poenibrojanje);;*

and I get all time this reuslt: 09-22 09:09:14.940: INFO/System.out(289): Number of points:0

Siten
  • 4,515
  • 9
  • 39
  • 64
Zookey
  • 2,637
  • 13
  • 46
  • 80

3 Answers3

4

Try this:

Intent intent = new Intent(this, RankActivity.class);
intent.putExtra("points", pointsVar);
startActivity(intent);

In onCreate of RankActivity:

getIntent().getIntExtra("points", 0);
Knickedi
  • 8,742
  • 3
  • 43
  • 45
4

so you want to pass integer value from one activity to another activity? right...

try:

Intent intent = new Intent(currentclass.this, destination.class);

intent.putExtra("point", pointvalue);

startActivity(intent);

at destination activity:

final int getpoint = getIntent().getExtras().getInt("point");

This will solve your problem.

Siten
  • 4,515
  • 9
  • 39
  • 64
  • I have tried, but without brackets on getIntent, it cant be resolved, and when I write getIntent().getExtraInt("point"); I get this error: The method getExtraInt(String) is undefined for the type Intent. – Zookey Sep 22 '11 at 09:48
  • Have you extended class with Activity? – Siten Sep 22 '11 at 09:57
2

first of all make static variable like as public static int poenibrojanje; in your BrzoRacunanjeActivity.class file now you can use this variable in any other class like as

 BrzoRacunanjeActivity.poenibrojanje

or you can use putExtras(); method.

in you main activity.

Intent i = new Intent(this, RankActivity.class);
i.putExtra("Value",poenibrojanje);

in your next activity

int v = (getIntent().getExtras().getInt("Value")) ;
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • Well, that was first thing that i have tried. It seems that it works on regular Java, not on Android. – Zookey Sep 22 '11 at 09:50
  • it is working also in android.. and you can also use putExtras – Niranj Patel Sep 22 '11 at 09:52
  • It seems that it works with just putting: public static int poenibrojanje; Intent intent = new Intent(BrzoRacunanjeActivity.this, RankActivity.class); startActivity(intent); RankActivity: BrzoRacunanjeActivity a1 = new BrzoRacunanjeActivity(); System.out.println(a1.poenibrojanje); – Zookey Sep 22 '11 at 10:08
  • i think dont need to write **BrzoRacunanjeActivity a1 = new BrzoRacunanjeActivity();** but you can direct use like this **BrzoRacunanjeActivity.poenibrojanje** – Niranj Patel Sep 22 '11 at 10:12
  • Yeah, I know, but I think this way is a little bit faster. Anyway, thanks for help. Cheers :) – Zookey Sep 22 '11 at 10:18
  • If you have time, can you take look at this problem : http://stackoverflow.com/questions/7515967/alertdialog-with-counddowntimer-android-help – Zookey Sep 22 '11 at 14:30