我已经在StackOverflow以及Google上阅读了很多类似的情况,但在我的案例中,他们都没有帮助过我。我有两个活动,A和B.A调用B,结果为活动:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mnuMainSettings:
startActivity(new Intent(ActivityMain.this, ActivitySettings.class));
break;
case R.id.mnuMainHelp:
startActivity(new Intent(ActivityMain.this, ActivityHelp.class));
break;
case R.id.mnuMainEULA:
Intent intent = new Intent(ActivityMain.this, ActivityDisclaimer.class);
startActivityForResult(intent, 1);
break;
}
return true;
}
活动B:
public class ActivityDisclaimer extends Activity
{
private WebView webView;
public static final int ACTIVITY_RESULT_DISCLAIMER = 1;
public static final String ACTIVITY_RESULT = "result";
public static final int ACTIVITY_RESULT_OK = 1;
public static final int ACTIVITY_RESULT_CANCELED = 2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.disclaimer);
webView = (WebView) findViewById(R.id.WebViewDisclaimer);
webView.loadUrl("file:///android_asset/disclaimer.html");
}
public void onClickDisclaimerBtnAccept(View view) {
Prefs prefs = new Prefs(this);
prefs.setAcceptDisclaimer(true);
prefs.commit();
Intent returnIntent = new Intent();
returnIntent.putExtra("result",true);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
public void onClickDisclaimerBtnDecline(View view) {
Prefs prefs = new Prefs(this);
prefs.setAcceptDisclaimer(false);
prefs.commit();
Intent returnIntent = new Intent();
returnIntent.putExtra("result",false);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
}
但是,我的onActivityResult方法,带有intent的返回数据总是为NULL。
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
// For response from disclaimer activity
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK){
if (null != intent) {
Boolean accepted = intent.getBooleanExtra("result", false);
Log.d(TAG, "Boolean Data: " + accepted.toString());
if (!accepted) finish();
} else { Log.d(TAG, "RESULT OK, DATA NULL"); }
}
if (resultCode == Activity.RESULT_CANCELED){
if (null != intent) {
Boolean accepted = intent.getBooleanExtra("result", false);
Log.d(TAG, "Boolean Data: " + accepted.toString());
finish();
} else { Log.d(TAG, "RESULT CANCELED, DATA NULL"); }
}
}
}//onActivityResult
“结果已取消,数据为空”是我得到的全部内容。我错过了什么吗?也许我已经盯着这段代码太久了!
编辑:为了更具体地回答我的问题,为什么我得到空回报?
答案 0 :(得分:6)
热狗!要回答我自己的问题,上面的代码很好。我终于找到了答案:fragments startActivityForResult always return resultCode 0 and intent null on callback onActivityResult
总结一下,我有android:launchMode =" singleInstance"在我的manifest.xml中。我删除了这一行,现在一切都很好!
真是一种解脱!