Using integer from one class in another Android - android

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

Try this:
Intent intent = new Intent(this, RankActivity.class);
intent.putExtra("points", pointsVar);
startActivity(intent);
In onCreate of RankActivity:
getIntent().getIntExtra("points", 0);

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.

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")) ;

Related

Android app seems hanging

I am trying to move from one activity to another activity. But some times seems it is hanging. On screen off and On it is working fine.
eg:
I am in Activity 1
Moved from Activity 1 to Activity 2
Seems it is hanged.
But if I do screen off and on it is on Activity 1.
Below is my Code sample:
Intent i;
i = new Intent(AdvSeatAvail.this, TrainsBetweenTwoStationsList.class);
i.putExtra("anim id in", R.anim.fragment_slide_right_enter);
i.putExtra("anim id out", R.anim.fragment_slide_left_exit);
i.putExtra("jsonvalue", jsonvalue);
i.putExtra("dateval", dateval);
AdvSeatAvail.this.finish();
AdvSeatAvail.this.startActivity(i);
overridePendingTransition(R.anim.fragment_slide_right_enter, R.anim.fragment_slide_left_exit);
Some one please help me to resolve this Issue.
--
thanks,
Kiran
Use below lines of code for it..
Firstly call another activity than finish the class
Intent i;
i = new Intent(AdvSeatAvail.this, TrainsBetweenTwoStationsList.class);
i.putExtra("anim id in", R.anim.fragment_slide_right_enter);
i.putExtra("anim id out", R.anim.fragment_slide_left_exit);
i.putExtra("jsonvalue", jsonvalue);
i.putExtra("dateval", dateval);
AdvSeatAvail.this.startActivity(i); //// FIRTLY START THE CLASS AND THAN FINISH THE CLASS
AdvSeatAvail.this.finish();
overridePendingTransition(R.anim.fragment_slide_right_enter, R.anim.fragment_slide_left_exit);
Try this:
finish();
Intent i = new Intent(AdvSeatAvail.this, TrainsBetweenTwoStationsList.class);
/* putExtra stuff */
startActivity(i);
Hopefuly it will help.

Passing multiple data series to activity/service

I know how to pass data from one activity to another using Intent:
Intent intent = new Intent(getBaseContext(), AddTextNote.class);
intent.putExtra("text", note.text);
I'll ask my question with example: If one activity would pass only a string to another, the second activity will know that everytime it receives intent from the first activity, it contains only a string. But if the first activity passes multiple set of data on various occations to another, what is the best way to distinguish them? I mean let's say first activity will pass an integer if user presses button1, will pass an array of customClass if user presses button2 and will pas float if user presses button3. So briefly there is 3 ways to start the second activity.
How should I know what intent contains? I can check for being null, like:
string myStringParameter = intent.GetStringExtra("myStringParameter");
But it is a not a wise and efficient way. Is there anyway to distinguish them fast and effective?
Thanks
I think that way could do the trick:
public enum State {
BUTTON_1, BUTTON_2, BUTTON_3
}
public class Activity1 extends Activity {
Intent intent = new Intent(getBaseContext(), Activity2.class);
intent.putExtra("text", note.text);
intent.putExtra("state", BUTTON_1);
}
public class Activity2 extends Activity {
State s = (State) intent.getSerializableExtra("state")
switch (s) {
case BUTTON_1:
//get String value from intent and do what needed
break;
case BUTTON_2:
//etc
break;
....
}
}
Hope you got the idea.
Data is identified via strings.
Activity A sends:
intent.putExtra("stringA", ""+BUTTON_A);
intent.putExtra("stringB", ""+BUTTON_B);
Activity B receives:
String stringAdata = getIntent().getStringExtra("stringA");
String stringBdata = getIntent().getStringExtra("stringB");

Using Integers, Strings etc in other Activities

I've got two Activities. In one I calculate some things and therefore I have some Integers and also some Strings stored. So now I would like to use these Stings and Integers in my second Activity. How an I use them in my second Activity?
thanks
You gotta pass them to your second activity.
When you create Intent to start activity
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
do extra step like this
i.putExtra("integer", myInteger); //where myInteger is integer you have in first Activity
i.putExtra("string", myString); //where myString is String you have in first Activity
startActivity(i);
and now in second Activity to access those values use following code
int in = this.getIntent().getExtras().getInt("integer");
String str = this.getIntent().getExtras().getString("string");
as simple as that

Running two activities at the same time on the screen

I need help doing something that I am sure is simple, but I can't figure out how to do it. I have a counter down and when it gets to the last 60 seconds, it calls a 'lastminute' counter activity. The plan is to overlap the last 60 seconds over the actual application.
Here is the problem, how can I change the code to allow the two activities to start at the same time.
I have tried this;
public void onFinish() {
startActivity(new Intent ("eu.merso.phoneapp.LASTMINUTE"));
startActivity(new Intent ("eu.merso.phoneapp.DASHBOARD"));
onDestroy();
}
but this does not put both applications on the screen, what I want is DASHBOARD on the background and LASTMINUTE on top. LASTMINUTE is alreay a "transparency colour".
Thanks;
Ramón
It won't work the way you're currently trying to do it. There can only be one visible activity at a time.
You should first start the dashboard Activity and from there you should start lastminute.
Edit --
Use a Bundle object.
Bundle bundle = new Bundle();
// Use 0 when the activity is called by the button and
// 1 when it is called by the timer.
bundle.putInt("event_src", 0);
intentObject.putExtras(bundle);
// In your new activity you can then check whether to display
// the countdown or not
Int eventSrc = getIntent().getExtras().getInt("event_src")
You need to implement the lastminute functionality in a dialog that you create and show in the onCreate method of your dashboard activity.
EDIT:
To distinguish between which activity that starts the new activity, use intent extras:
//in your calling activity
Intent i = new Intent(A.this, B.class);
i.putExtra("from Activity", A.class.getSimpleName());
startActivity(i);
//in your receiving activity
String from = getIntent().getStringExtra();
if(from.equals(A.class.getSimpleName())){
//do something
}
else if(from.equals(C.class.getSimpleName())){
//do something
}
Try using android:theme="#android:style/Theme.Translucent.NoTitleBar in Activity attributes for LastMinute in AndroidManifest.xml. I hope it'll be productive.
I think using Android fragments will help u to show two separate activities in the context of another main activity.
try reading this:
http://developer.android.com/guide/components/fragments.html

Changing Screen / New Intent

I am trying to change screens from the first to second and back again, but with some extra variables passed. At the moment I go from 1 to 2, but when I click from 2 back to 1 it causes an error that the application stopped unexpectantly. I have issolated the problem to this part of the code. I'm not at the moment trying to pass any other variables with it just get it to change.
1st page package = max.multiplebuttons.com
Activity = multibuttons
1st page package = max.reason.com
Activity = reason_screen
public void nextquestion(){
Intent a = new Intent();
a.setClassName("max.reason.com", "max.multiplebuttons.com.multibuttons");
startActivity(a);
}
a.setClassName("max.reason.com", "max.multiplebuttons.com.multibuttons");
Your problem is here. The class multibuttons isn't inside the package max.reason.com.
Edit:
I can't give you the exact syntax because I need to see the naming conventions, but you have to do something like this:
Intent a = new Intent(Activity1.this, Activity2.class);
startActivity(a);

Categories

Resources