简体   繁体   English

通过多个活动传递数据

[英]Passing data though multiple activities

1- is my first activity(main) 2- is my second activity 3 - is my third activity 1-是我的第一个活动(主要)2-是我的第二个活动3 - 是我的第三个活动

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. 我想从1开始运行2然后运行2运行3,然后从3运行数据并将其返回到1.希望你们明白的人。

Here is my code: 这是我的代码:

Runing 2 form 1 liek this : Runing 2 form 1 liek:

            Intent intent = new Intent(getApplicationContext(),MessageBox.class);
             intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
            startActivityForResult(intent,5);               

And then running 3 from 2 like this: 然后像这样从2运行3:

                Intent intent = new Intent(getApplicationContext(),ImageReceiver.class);

                startActivityForResult(intent,5);

And then in 3 i have something like this: 然后在3我有这样的事情:

          setResult(10);
          finish();

So i set result so in 2 to get this result i have: 所以我在2中设置结果得到这个结果我有:

        if(requestCode==5)
        {
            if(resultCode==10)
            {

                Intent intent = new Intent(getApplicationContext(),MainActivity.class);

                setResult(5,intent);
                finish();
            }
        }

And then in 1 i got: 然后在1我得到:

     if(requestCode==5)
     {
         if(resultCode==5)
         {
             //here i am taking data from 3
         }
     }

Problem is i cant even open 2 coz in logcat i am getting: 问题是我甚至无法在logcat中打开2 coz我得到:

04-23 22:13:15.579: E/AndroidRuntime(15313): android.util.AndroidRuntimeException: FORWARD_RESULT_FLAG used while also requesting a result 04-23 22:13:15.579:E / AndroidRuntime(15313):android.util.AndroidRuntimeException:使用FORWARD_RESULT_FLAG同时请求结果

And i dont really get what should i do. 我真的不知道该怎么办。 Please look at this code. 请看这个代码。

You cannot do this when starting 2 from 1: 从1开始2时你不能这样做:

Intent intent = new Intent(getApplicationContext(), MessageBox.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivityForResult(intent,5);

This will throw the exception you are getting. 这将抛出你得到的异常。 You cannot use FLAG_ACTIVITY_FORWARD_RESULT and startActivityForResult() together. 您不能一起使用FLAG_ACTIVITY_FORWARD_RESULTstartActivityForResult()

If you want 1 to get the result from 3, then you need to start 2 from 1 like this: 如果你想1得到3的结果,那么你需要从1开始2,如下所示:

Intent intent = new Intent(getApplicationContext(), MessageBox.class);
startActivityForResult(intent, 5);

and then start 3 from 2 like this: 然后像这样从2开始3:

Intent intent = new Intent(getApplicationContext(), ImageReceiver.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(intent);
finish();

This tells Android that activity 3 ( ImageReceiver ) should forward its result back to the activity that called activity 2 ( MessageBox ). 这告诉Android活动3( ImageReceiver )应该将其结果转发回调用活动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. 当活动3设置其结果并结束时,将使用从活动3发送的结果数据调用活动1中的onActivityResult()

comment out this line in activity 1 在活动1中注释掉这一行

intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);

and this line in activity 2 这一行在活动2中

Intent intent = new Intent(getApplicationContext(),MainActivity.class);

You should be all set. 你应该全力以赴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM