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?
Try
Insted of