问题描述
使用简单的应用程序,我遇到了一个大而令人沮丧的问题.我有两个活动-A和B.活动A在单击按钮时打开活动B.活动B有两个结束":如果用户单击B.返回,如果刚刚结束并返回到A,如果用户单击B.OK按钮,则活动B已完成,但在此之前将结果设置为确定",因此可以关闭活动A也是.
I have a big and frustrating problem with a simple application.I have two Activities - A and B. Activity A opens activity B on a button click. Activity B has two "ends": If user clicks B.Back if just finish and goes back to A, if user clicks B.OK button, activity B is finished but before that it sets result "OK" so activity A can be closed too.
这是A的伪代码:
btnNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//some code...
startAct(data);
}
});
private void startAct(Intent inte) {
startActivityForResult(inte, -999);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println(resultCode);
if(requestCode != -999)
return;
if(resultCode == RESULT_OK)
finish();
}
活动B这样做:
btnOk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//some other code...
finishOK();
}
});
private void finishOK() {
setResult(RESULT_OK, new Intent());
finish();
}
问题是,这将不起作用-当我按"btnOK"时,B应该完成,A应该也捕获"结果并完成,但是根本不会发生.我想念什么?
The problem is that this won't work - when i press the "btnOK" B should finish, A should "catch" the result and finish too, but it simply won't happen. What am i missing?
推荐答案
您必须使用非负数requestCode
.如果requestCode
为负,则startActivityForResult
与startActivity
相同.请参阅官方文档 http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent,%20int)
You have to use a non-negative requestCode
. If the requestCode
is negative then startActivityForResult
is the same as startActivity
. See official document http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent,%20int)
这篇关于Android的"onActivityResult"机制不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!