我有三个活动,A,B和C(下面的代码)。 A用startActivityforResult调用B,并覆盖onActivityResult,B也用ActivityforResult调用C,并且onActivityResult将结果返回给A。我的问题是onActivityResult没有被触发。我认为这是由于我要从onCreate之外的函数完成Activity,但我没有头绪。
我尝试寻找其他答案,但没有成功。
我已经尝试过Intents,但似乎都没有用。
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()可以正常工作,但是不会触发AddRecActivity onResultActivity,因此此Activity不会完成,MainActivity也不会得到结果。
预期的行为:C完成,B触发onResultActivity并完成,A触发onResultActivity并刷新活动。
答案 0 :(得分:1)
StartActivityForResult将意图和requestCode作为参数。您正在传递RESULT_OK。尝试每次创建一个唯一的代码(它可以是任何整数),然后在onActivityResult上查找该特定的requestCode。
Intent i = new Intent(ActivityA.this, ActivityB.class);
startActivityForResult(i, 100); //I'm using 100 as a requestCode
如果要在ActivityB上将结果设置为“确定”:
Intent returnIntent = new Intent(); //use this intent if you want to send dat back to the other activity
setResult(Activity.RESULT_OK,returnIntent);
finish();
及其答案:
@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
}
}
}
答案 1 :(得分:1)
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