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

4
  • so you want to pass integer value from one activity to another activity? Commented Sep 22, 2011 at 9:32
  • I updated my answer check it will work. Commented Sep 22, 2011 at 9:47
  • remove BrzoRacunanjeActivity a1 = new BrzoRacunanjeActivity();* Commented Sep 22, 2011 at 10:39
  • Hey dude, if you have time, can you take look at this problem: stackoverflow.com/questions/7515967/… Commented Sep 23, 2011 at 10:06

3 Answers 3

4

Try this:

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

In onCreate of RankActivity:

getIntent().getIntExtra("points", 0);
Sign up to request clarification or add additional context in comments.

3 Comments

I'm sorry for that, it's getIntent(). Hmpf and not getExtraInt. I corrected that...
When I put brackets on getIntent,I get this error: The method getExtraInt(String) is undefined for the type Intent
Sorry again, see comment obove your's
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.

2 Comments

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.
Have you extended class with Activity?
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")) ;

6 Comments

Well, that was first thing that i have tried. It seems that it works on regular Java, not on Android.
it is working also in android.. and you can also use putExtras
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);
i think dont need to write BrzoRacunanjeActivity a1 = new BrzoRacunanjeActivity(); but you can direct use like this BrzoRacunanjeActivity.poenibrojanje
Yeah, I know, but I think this way is a little bit faster. Anyway, thanks for help. Cheers :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.