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
Related
This question already has answers here:
How to manage startActivityForResult on Android
(14 answers)
Closed 6 years ago.
From now I just put:
finish();
but it only makes me return to the parent activity and I want to pass values. Thanks in advance.
If you want to open another activity sending some string or anything use:
Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
intent.putExtra("ReferencedWord","String With Whatever You Want");
int CodigoPeticion;
CodigoPeticion=2;
startActivityForResult (intent,CodigoPeticion);
finish();
referencedWord is the key to open the string in the other activity.
In Another Activity use this in your onCreate
String word="";
Bundle extras = getIntent().getExtras();
if (extras!= null) {
word = extras.getString("ReferencedWord");
Toast.makeText(getBaseContext(),"String given by MainActivity: "+word,Toast.LENGTH_LONG).show();
}
when you go form first activity to second activity.
the first activity become pause.
so when you finish the second activity the first activity is again resume
you want to pass some value from second activity to first activity
so just finish the first activity and start second activity
when you are coming back to first activity just intent from second to first activity and attached the bundle message to it after it finish the second activity
If you wanna pass any values from one activity to another activity
Intent sendVal = new Intent(this,NextActivity.class);
sendVal.putExtra("key","any values"); //key is your private key for sending values
startActivityForResult(sendVal,4); //request code
finish();
In next Activity,you can retrieve those values from PreviousActivity :
Bundle extraVal = getIntent().getExtras();
//getting values from extraVal
String myString = extraVal.getString("key"); //so our value is "any values"
Toast.makeText(getApplicationContext(),"Your string from previous Activity"+myString,Toast.LENGTH_LONG).show();
By this way,you can retrieve values from PreviousActivity.
If you wanna get values from NextActivity to PreviousActivity then override the onActivityResult method and retrieve your values from intent parameter(3rd) of onActivityResult method by matching your request code.
onActivityResult method invokes when you come back from NextActivity to PreviousActivity
I want to pass a value from the ListView Activity 1 Activity 2 for editing.
I have this code but the value is not passed in the second Activity.
ACTIVITY A
Intent i = new Intent(this, Modifica_entrate.class);
Bundle extras = new Bundle();
extras.putString (tv1.getText().toString(), data);
i.putExtras(extras);
ACTIVITY B
Bundle extras = getIntent().getExtras();
String valuePass = extras.getString("data");
mDataScelta.setText(i.getExtras().getString(valuePass));
You are mixing up keys and values a bit too much.
The first parameter here:
extras.putString (tv1.getText().toString(), data);
Must match the parameter here:
String valuePass = extras.getString("data");
So the code you have there puts a String with the key tv1.getText().toString() that is, it takes the text you entered in the textbox and uses it as a key (which is probably not what you intended to do). For this key, you are putting the value of the variable data. Then you try to retrieve the key "data" (note also that data and "data" are not the same thing).
So what you want is probably:
extras.putString("data", tv1.getText().toString());
And then you can retrieve it like this:
mDataScelta.setText(i.getStringExtra("data"));
I have two activities say X and y. In x there is edittext n 6 radiobutton if user clicks button the value gets retrieved from database based on input from edittext n radiobutton. the values should be displayed in next activity y. can yu pls help in giving snippet..thanks in advance
You can easily pass data from one activity to another using Bundle or Intent.
Lets look at the following example using Bundle:
//creating an intent to call the next activity
Intent i = new Intent("com.example.NextActivity");
Bundle b = new Bundle();
//This is where we put the data, you can basically pass any
//type of data, whether its string, int, array, object
//In this example we put a string
//The param would be a Key and Value, Key would be "Name"
//value would be "John"
b.putString("Name", "John");
//we put the bundle to the Intent
i.putExtra(b);
startActivity(i, 0);
In "NextActivity" you can retrieve the data using the following code:
Bundle b = getIntent().getExtra();
//you retrieve the data using the Key, which is "Name" in our case
String data = b.getString("Name");
How about using only Intent to transfer data. Lets look at the example
Intent i = new Intent("com.example.NextActivity");
int highestScore = 405;
i.putExtra("score", highestScore);
In "NextActivity" you can retrieve the data:
int highestScore = getIntent().getIntExtra("score");
Now you would ask me, whats the difference between Intent and Bundle, they seems like
they do exactly the same thing.
The answer is yes, they both do exactly the same thing. But if you want to transfer alot of data, variables, large array you would need to use Bundle, as they have more methods for transferring large amount of data.(i.e. If your only passing one or two variable then just go with Intent.
You should put the values into a bundle and pass that bundle to the intent that's starting the next activity. Sample code is in the answer to this question: Passing a Bundle on startActivity()?
Bind the data that you would like to send with the Intent you are calling to go to the next activity.
Intent i = new Intent(this, YourNextClass.class);
i.putExtra("yourKey", "yourKeyValue");
startActivity(i);
In YourNextClass activity, you can get the passed data by using
Bundle extras = getIntent().getExtras();
if (extras != null) {
String data = extras.getString("yourKey");
}
I want to transfer a String value from one class to another class in same package. The second class is called using intent in onclick of a menu item. I used the code
Intent i = new Intent(TouristGuideActivity.this, PointOfInterest.class);
i.putExtra("videoId", videoId);
startActivity(i);
in first class and then in second class,
String address=getIntent().getExtras().getString("videoId");`
But when I click on the menu item, I get a force close. If I remove that put Extra part, it works ine. But in that case I can't send the string. Please help!
Intent intent=new Intent(this,secondclass.class);
intent.putExtra("videoId",videoId);
startActivity(intent);
and in your second class try 2 get these items by doing
Bundle extras = getIntent().getExtras();
if(extras!=null){
String videoId=extras.getString("videoId");
}
just after u define your class i hope dis would help u out 2 get the values on other page
Make sure that you are getting the same type, for example, if you put a String, get a String.
Then try with:
getIntent().getStringExtra("videoId");
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")) ;