我无法解释您的代码中发生了什么,但我有一个项目示例来执行此操作..
一个只有一个按钮 btnFoo 的 FooActivity:
@Override
protected void onStart() {
super.onStart();
btnFoo.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent("xper.activity.ACTIVITY_BAR_INTENT"),1);
}
});
}
并在 AndroidManifest.xml 中添加了一个 BarActivity,如下所示:
<activity
android:name = "BarActivity">
<intent-filter>
<action
android:name = "xper.activity.ACTIVITY_BAR_INTENT"/>
<category
android:name = "android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
检索条内意图的相应代码位于 btnBar (Button) 的 onClicEvent 中:
@Override
protected void onStart() {
super.onStart();
btnBar.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("xper.activity.ACTIVITY_BAR_RESULT_INTENT");
intent.putExtra("codBar", "bar");
setResult(Activity.RESULT_OK, intent);
finish();
}
});
}
现在,如果您没有很好地处理 onActivityResult() 事件,当您按下 Android 按钮“BACK”时,您可能会收到错误消息。
如果Activity B中的Intent(意图)是给Activity A一些信息,如果你按下按钮返回,不知道Activity B会不会入栈,但是Intent没有做。所以我做了以下事情:
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
//Intent intent = new Intent("xper.activity.ACTIVITY_BAR_RESULT_INTENT");
//intent.putExtra("codBar", "bar");
//setResult(Activity.RESULT_CANCELED, intent);
setResult(Activity.RESULT_CANCELED);
finish();
}
处理信息我在事件 onActivityResult() 中做了以下操作,以查看 Bar Activity 中检索到的信息:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data != null) {
Toast.makeText(this, "BAR\trequestCode == " + requestCode + "\tresultCode == " + resultCode + "\tdata == " + data, 10000).show();
btnFoo.setText("BAR\trequestCode == " + requestCode + "\tresultCode == " + resultCode + "\tdata == " + data /*+ "extras == " + data.getExtras().getString("codBar")*/);
} else {
Toast.makeText(this, "BAR\trequestCode == " + requestCode + "\tresultCode == " + resultCode, 10000).show();
btnFoo.setText("BAR\trequestCode == " + requestCode + "\tresultCode == " + resultCode);
}
}
如果您有更多活动将信息返回给父活动是很好的做法,请执行以下操作:
//put private static final int globals atributes with the respective name of the
//activity to represent the requestCode for each activity you have like:
private static final int ACTIVITY1 = 117;
private static final int ACTIVITY2 = 118;
...
private static final int ACTIVITYN = 215;
//In the event onActivityResult() is better use the switch statement to handle each
//specific activity to catch information
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_CANCELED) return; // breaks
//if you decide to handle some information of Activity.RESULT_CANCELED
//ignore the above condition that returns and handle it inside the switch statement
switch(requestCode) {
case ACTIVITY1:
{
//Dosomething
} break;
case ACTIVITY2:
{
//Dosomething
} break;
...
case ACTIVITYN:
{
//Dosomething
} break;
}
}
如果您不能执行此示例代码.. 请给我您的电子邮件,让我发送 FooBarActivity 项目