Reputation: 1246
I want to sent intent from one first activity to another.
The first activity sends an intent to the second activity in order to create a new AlertDialog, receive a number from the user and send the number back to the first activity, that's where putExtra data to failed.
The code of the first activity: GuessItActivity.java
Intent intent = new Intent(GuessItActivity.this,
AlertDialogMessage.class);
intent.addFlags(Intent.FILL_IN_DATA);
intent.addFlags(Intent.FILL_IN_CATEGORIES);
intent.addFlags(Intent.FILL_IN_ACTION);
intent.putExtra("data1", 15);
startActivityForResult(
intent,
getResources().getInteger(
R.integer.ALERT_DIALOG_MESSAGE));
on the retriever side , e.g the second activity side on AlertDialogMessage.java
public void onClick(DialogInterface dialog,
int which) {
// "OK" button pressed
int userGuess = Integer.parseInt(input
.getText().toString());
Intent intent = getIntent();
if (intent == null)
return;
int data1 = intent.getIntExtra("data1", -1);
if (data1 != 15 )
return; // data1 ==15
intent.putExtra("data2", 25);
if (getParent() == null) {
setResult(Activity.RESULT_OK, intent);
} else {
getParent().setResult(Activity.RESULT_OK,
intent);
}
finish();
The first activity side: back to GuessItActivity.java
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == getResources().getInteger(
R.integer.ALERT_DIALOG_MESSAGE)) {
if (resultCode == RESULT_OK) {
Intent intent = getIntent();
if (intent==null)
return;
Bundle extra = getIntent().getExtras();
if (extra != null) {
int _data1 = extra.getInt("data1");
int _data2 = extra.getInt("data2");
}
// extra == null , what am I doing wrong ?
int data1 = intent.getIntExtra("data1",-1);
int data2 = intent.getIntExtra("data2",-1);
if ((data1==-1)&&(data1==-1))
return;
The problem is , that I receive data1 and data2 equal to -1.
I'd like to receive the data the I put on the second activity. e.g data1 == 15 and data2 == 25
What am I doing wrong?
Upvotes: 3
Views: 5649
Reputation: 3469
in onActivityResult you are not suppposed to call
intent = getIntent();
directly you have got the
protected void onActivityResult(int requestCode, int resultCode, Intent data)
where data corresponds to the data sent from the alert box activity. Just directly calling
data.getIntExtra("data2", -1);
should do. Hope I was clear enough.
Upvotes: 0
Reputation: 3868
Try
Bundle extra=data.getExtras();
Insted of
Bundle extra=getIntent().getExtras();
Upvotes: 3
Reputation: 792
try with this.
Bundle extras = getIntent().getExtras();
int a=extras.getInt("data1");
Upvotes: 0
Reputation: 7605
try this way
int data1,data2;
Bundle extra=getIntent().getExtras();
if(extra!=null){
data1=extra.getInt("data1");
data2=extra.getInt("data2");
}
Upvotes: 0