Reputation: 93
I have three activities, A, B and C (code below). A calls B with startActivityforResult with an override for onActivityResult, B calls C with ActivityforResult too and onActivityResult to return the result to A. My problem is that onActivityResult is not being triggered. I think its due to I'm finishing the Activity from an function outside the onCreate, but I have no clue.
I've tried looking into another answers but didn't make it work.
I've tried with Intents but none of that seems to work.
MainActivity.java
Button btn = findViewById(R.id.add);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addReminder();
}
});
} //Finish of onCreate
private void addReminder(){
Intent intent = new Intent(this, AddRecActivity.class);
startActivityForResult(intent, RESULT_OK);
public void refresh() {
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
refresh();
}
}
AddRecActivity.java
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
cursor.moveToPosition(position);
Medicina med = new Medicina(cursor);
Intent intent = new Intent(AddRecActivity.this, RecFinishActivity.class);
intent.putExtra("NAME", med.getName());
intent.putExtra("QUANTITY", med.getQuantity());
intent.putExtra("TYPE", med.getType());
intent.putExtra("PILLS", med.getPills());
startActivityForResult(intent, RESULT_OK);
}
});
}//onCreate end here
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(RESULT_OK == resultCode){
Log.i("Insercion:", "Insercion correcta");
} else {
Log.i("Insercion:", "Insercion fallida");
}
setResult(RESULT_OK);
finish();
}
RecFinishActivity.java
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addRec(data.getString("NAME"), data.getInt("QUANTITY"), data.getString("TYPE"),
data.getInt("PILLS"));
}
});
} //onCreate end here
void addRec(String name, int quantity, String type, int pills){
Consumo con = new Consumo(name, quantity, type, pills, true, Integer.parseInt(min.getText().toString()));
long success = sRegistrosDbHelper.saveConsumo(con);
setResult(RESULT_OK);
finish();
}
RecFinishActivity finish() is working, but AddRecActivity onResultActivity is not triggered, so this Activity doesn't finish and MainActivity doesn't get the result.
Expected behaviour: C finishes, B triggers onResultActivity and finish, A triggers onResultActivity and Refresh the Activity.
Upvotes: 0
Views: 343
Reputation: 427
here your problem is startActivityForResult(intent, RESULT_OK); , the value of RESULT_OK is -1 which is ignored by internal call from startActivityforResult use any other request code like
MainActivity :`
Button btn = findViewById(R.id.add);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addReminder();
}
});
} //Finish of onCreate
private void addReminder(){
Intent intent = new Intent(this, AddRecActivity.class);
startActivityForResult(intent, 100);
public void refresh() {
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
refresh();
}
}
`AddRecActivity.java`
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
cursor.moveToPosition(position);
Medicina med = new Medicina(cursor);
Intent intent = new Intent(AddRecActivity.this, RecFinishActivity.class);
intent.putExtra("NAME", med.getName());
intent.putExtra("QUANTITY", med.getQuantity());
intent.putExtra("TYPE", med.getType());
intent.putExtra("PILLS", med.getPills());
startActivityForResult(intent, 100);
}
});
}//onCreate end here
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(100 == requestCode){
Log.i("Insercion:", "Insercion correcta");
} else {
Log.i("Insercion:", "Insercion fallida");
}
setResult(RESULT_OK);
finish();
}
`RecFinishActivity.java
`
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addRec(data.getString("NAME"), data.getInt("QUANTITY"), data.getString("TYPE"),
data.getInt("PILLS"));
}
});
} //onCreate end here
void addRec(String name, int quantity, String type, int pills){
Consumo con = new Consumo(name, quantity, type, pills, true, Integer.parseInt(min.getText().toString()));
long success = sRegistrosDbHelper.saveConsumo(con);
setResult(RESULT_OK);
finish();
}`
you can use any value in request code other than 100,101, non negative value
Upvotes: 1
Reputation: 3766
StartActivityForResult takes in the intent and a requestCode as a parameter. You're passing the RESULT_OK instead. Try creating a unique code each time (it could be any integer), and on your onActivityResult look for that specific requestCode.
Intent i = new Intent(ActivityA.this, ActivityB.class);
startActivityForResult(i, 100); //I'm using 100 as a requestCode
if on ActivityB you want to set the result as OK:
Intent returnIntent = new Intent(); //use this intent if you want to send dat back to the other activity
setResult(Activity.RESULT_OK,returnIntent);
finish();
and on answer to that:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
if(resultCode == Activity.RESULT_OK){
//do something
}
if (resultCode == Activity.RESULT_CANCELED) {
//do something if there's no result
}
}
}
Upvotes: 2