Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data though multiple activities

1- is my first activity(main) 2- is my second activity 3 - is my third activity

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.

Here is my code:

Runing 2 form 1 liek this :

            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:

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

                startActivityForResult(intent,5);

And then in 3 i have something like this:

          setResult(10);
          finish();

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();
            }
        }

And then in 1 i got:

     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:

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.

like image 384
user3465277 Avatar asked Apr 23 '14 20:04

user3465277


2 Answers

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);

This will throw the exception you are getting. You cannot use FLAG_ACTIVITY_FORWARD_RESULT and startActivityForResult() together.

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);

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();

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.

like image 66
David Wasser Avatar answered Sep 16 '22 20:09

David Wasser


comment out this line in activity 1

intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);

and this line in activity 2

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

You should be all set.

like image 33
itsben Avatar answered Sep 18 '22 20:09

itsben