英文:
OnActivityResult() returning null data
问题
我知道这个问题已经被问过很多次了,我按照所有步骤正确地进行了操作,但在我的FirstApplication中仍然得到了空数据。
FirstApplication通过使用在构造函数中使用的activity变量来启动一个意图,从一个库类中开始获得结果:
sendIntent.putExtra("data", bundle);
sendIntent.setType("text/plain");
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (sendIntent.resolveActivity(context.getPackageManager()) != null) {
activity.startActivityForResult(sendIntent, requestCode);
}
SecondApplication获取意图并执行必要的操作:
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("data");
if (bundle!=null){
type = bundle.getString("type");
num1 = Integer.parseInt(bundle.getString("num1"));
num2 = Integer.parseInt(bundle.getString("num2"));
if (type.equalsIgnoreCase("add")){
result = String.valueOf(num1+num2);
}else {
result = String.valueOf(num1-num2);
}
}
将结果发送到FirstApplication:
Bundle bundle1 = new Bundle();
if (bundle1!=null){
Intent send = new Intent();
bundle1.putString("result", result);
send.putExtra("datasend", bundle1);
setResult(Activity.RESULT_OK, send);
finish();
}
在FirstApplication的MainActivity中接收结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getBundleExtra("datasend");
result = bundle.getString("result");
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
但是在这里,我收到了结果码为0和数据为空。任何帮助将不胜感激。谢谢。
英文:
I know this question has been asked many times and i followed all the steps correctly but still getting null data in my FirstApplication
FirstApplication starting an intent to get result from a library class using Mainactivity that is initialised using the activity variable in a constructor:
sendIntent.putExtra("data", bundle);
sendIntent.setType("text/plain");
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (sendIntent.resolveActivity(context.getPackageManager()) != null) {
activity.startActivityForResult(sendIntent, requestCode);
}
SecondApplication getting the intent and doing necessary operation
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("data");
if (bundle!=null){
type = bundle.getString("type");
num1 = Integer.parseInt(bundle.getString("num1"));
num2 = Integer.parseInt(bundle.getString("num2"));
if (type.equalsIgnoreCase("add")){
result = String.valueOf(num1+num2);
}else {
result = String.valueOf(num1-num2);
}
}
Sending result to FirstApplication
Bundle bundle1 = new Bundle();
if (bundle1!=null){
Intent send = new Intent();
bundle1.putString("result", result);
send.putExtra("datasend", bundle1);
setResult(Activity.RESULT_OK, send);
finish();
}
Receiving result in FirstApplication MAinActivity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getBundleExtra("datasend");
result = bundle.getString("result");
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
But here i am receiving resultCode 0 and data null. Any help will be appreciated. Thank you.
答案1
得分: 0
问题出在这一行
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
通过调用Intent.FLAG_ACTIVITY_NEW_TASK
,你将第二个活动置于返回堆栈的顶部,并取消了startActivityForResult()
的效果。
因此,应将其移除以恢复结果。
英文:
The problem is in this line
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
By calling Intent.FLAG_ACTIVITY_NEW_TASK
you are setting the second activity on the top of the back stack and cancelling the effect of startActivityForResult()
So it should be removed to get back the result
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论