通过多个活动传递数据 [英] Passing data though multiple activities
问题描述
1-是我的第一个活动(主) 2-是我的第二项活动 3-这是我的第三项活动
1- is my first activity(main) 2- is my second activity 3 - is my third activity
我想从1开始运行2,然后从3开始形成表格2,然后从3开始运行,我正在获取数据并将其返回给1.希望您能理解.
I want to run 2 from 1 and then form 2 run 3 , and then from 3 i am taking data and returning it to 1. Hope guys you will understand.
这是我的代码:
Runing 2 form 1 likes:
Runing 2 form 1 liek this :
Intent intent = new Intent(getApplicationContext(),MessageBox.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivityForResult(intent,5);
然后像这样从2运行3:
And then running 3 from 2 like this:
Intent intent = new Intent(getApplicationContext(),ImageReceiver.class);
startActivityForResult(intent,5);
然后在3中我有类似的东西:
And then in 3 i have something like this:
setResult(10);
finish();
所以我将结果设置为2,以获得以下结果:
So i set result so in 2 to get this result i have:
if(requestCode==5)
{
if(resultCode==10)
{
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
setResult(5,intent);
finish();
}
}
然后在1中我得到:
if(requestCode==5)
{
if(resultCode==5)
{
//here i am taking data from 3
}
}
问题是我什至无法在logcat中打开2个coz:
Problem is i cant even open 2 coz in logcat i am getting:
04-23 22:13:15.579:E/AndroidRuntime(15313):android.util.AndroidRuntimeException:同时请求结果时使用了FORWARD_RESULT_FLAG
04-23 22:13:15.579: E/AndroidRuntime(15313): android.util.AndroidRuntimeException: FORWARD_RESULT_FLAG used while also requesting a result
我真的不知道该怎么办.请看下面的代码.
And i dont really get what should i do. Please look at this code.
推荐答案
从1开始从2开始,您将无法执行此操作.
You cannot do this when starting 2 from 1:
Intent intent = new Intent(getApplicationContext(), MessageBox.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivityForResult(intent,5);
这将引发您得到的异常.您不能同时使用FLAG_ACTIVITY_FORWARD_RESULT
和startActivityForResult()
.
This will throw the exception you are getting. You cannot use FLAG_ACTIVITY_FORWARD_RESULT
and startActivityForResult()
together.
如果您希望1从3中得到结果,那么您需要像这样从1开始2:
If you want 1 to get the result from 3, then you need to start 2 from 1 like this:
Intent intent = new Intent(getApplicationContext(), MessageBox.class);
startActivityForResult(intent, 5);
然后像这样从2开始3:
and then start 3 from 2 like this:
Intent intent = new Intent(getApplicationContext(), ImageReceiver.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(intent);
finish();
这告诉Android活动3(ImageReceiver
)应该将其结果转发回称为活动2(MessageBox
)的活动.当活动3设置其结果并结束时,活动1中的onActivityResult()
将被调用,并带有活动3中发送的结果数据.
This tells Android that activity 3 (ImageReceiver
) should forward its result back to the activity that called activity 2 (MessageBox
). When activity 3 sets its result and finishes, onActivityResult()
in activity 1 will be called with the result data sent from activity 3.
这篇关于通过多个活动传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!