Reputation: 69
Hello everyone if you would kindly help me. I'm stuck on how to retrieve the bundle data from another activity. Basically I have two activities which is that when I pressed a button on the first activity, it will go on the second activity and then sets the string values which is later on to be passed on the first activity. What I did was I used the bundle to put the string values. My question is that how can I get the bundle values (of strings) from the second activity and return it on to the first activity? Here is my code:
FirstActivity (going on to second activity):
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(this,
SecondActivity.class), REQUEST_CODE_SAMPLE);
}
});
SecondActivity: (returning the bundle value)
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bundle b = new Bundle();
bundle.putString("A", "Aloha");
bundle.putString("B", "Bubbles");
setResult(
Activity.RESULT_OK,
getIntent().putExtras(b));
}
});
}
FirstActivity (going to retrieve the bundle values):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE_SAMPLE:
if (resultCode == RESULT_OK) {
bundle = getIntent().getExtras();
//WHAT TO DO TO GET THE BUNDLE VALUES//
String a = //STORE FIRST VALUE OF BUNDLE
String b = //STORE SECOND VALUE OF BUNDLE
}
break;
default:
break;
}
}
Upvotes: 0
Views: 5803
Reputation: 15973
You need to do the following:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE_SAMPLE:
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
//WHAT TO DO TO GET THE BUNDLE VALUES//
String a = bundle.getString("A");
String b = bundle.getString("B");
}
break;
}
}
but take care, you must use the intent passed to the onActivityResult
not getIntent
Also in SecondActivity, you need to use a new intent:
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle= new Bundle();
bundle.putString("A", "Aloha");
bundle.putString("B", "Bubbles");
Intent returnIntent = new Intent();
returnIntent.putExtras(bundle);
setResult(Activity.RESULT_OK, returnIntent);
}
});
}
Upvotes: 5