Get Data From Another Activity
Solution 1:
In your second activity, you can get the data from the first activity with the method getIntent()
and then getStringExtra()
, getIntExtra()
...
Then to return to your first activity you have to use the setResult()
method with the intent data to return back as parameter.
To get the returning data from your second activity in your first activity, just override the onActivityResult()
method and use the intent to get the data.
First Activity:
//In the method that is called when click on "update"Intentintent= ... //Create the intent to go in the second activity
intent.putExtra("oldValue", "valueYouWantToChange");
startActivityForResult(intent, someIntValue); //I always put 0 for someIntValue//In your class@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Retrieve data in the intentStringeditTextValue= intent.getStringExtra("valueId");
}
Second Activity:
//When activity is createdStringvalue= intent.getStringExtra("oldValue");
//Then change the editText value//After clicking on "save"Intentintent=newIntent();
intent.putExtra("valueId", value); //value should be your string from the edittext
setResult(somePositiveInt, intent); //The data you want to send back
finish(); //That's when you onActivityResult() in the first activity will be called
Don't forget to start your second activity with the startActivityForResult()
method.
Solution 2:
You have to pass the information as extras.
Passing the Information
Intenti=newIntent();
i.setClassName("com.example", "com.example.activity");
i.putExtra("identifier", VALUE);
startActivity(i);
Getting the Information
Bundleextras= getIntent().getExtras();
StringexampleString= extras.getString("identifier");
Solution 3:
When you want to start second activity, use startActivityForResult(your intent, request code);
In your first activity use
protectedvoidonActivityResult(int requestCode, int resultCode,
Intent data){
if (requestCode == your_reques_code) {
if (resultCode == RESULT_OK) {
// do your stuff
}
}
}
Before finish second activity dont forget this,
Intent data = new Intent();
data.putExtra("text", edtText.getText());
setResult(RESULT_OK, data);
Post a Comment for "Get Data From Another Activity"