> I'm just not getting the documentation on Intents.  I have two
> Activities A and B.  Activity A calls Activity B based on a menu
> selection.  This is my code snippet:
>
> ccase R.id.MENU_SELECTION:
>      Intent intent=new Intent(Intent.ACTION_ATTACH_DATA);
>      intent.setClass(MainClass.this,ReturnClass.class);
>      startActivityForResult(intent,Intent.FILL_IN_DATA);
>      break;
>
>  What I ultimately want is for Activity B to return a number to me.
> In the above, I select Intent.ACTION_ATTACH_DATA  mainly because I
> didn't know what else to select.

Try:

Intent intent=new Intent(MainClass.this, ReturnClass.this);

and skip the setClass() statement.

> The setClass specifies the class for
> Activity A and the destination class (the class for Activity B).  In
> the startActivityForResult, I pass the intent.  The
> Intent.FILL_IN_DATA is also because
>  I didn't know what else to select, but I do want data returned.

You need to pass in a locally-unique integer, so you can tell this
startActivityForResult() call from the 1337 other ones you might be making
in this activity. What the value is, beyond being unique within your
activity, is up to you.

>  In anticipation for the Result, I have added the following in my
> MainClass:
>
>  protected void OnActivityResult(int requestCode, int resultCode,
> Intent data)
>  {
>      if (requestCode==Intent.FILL_IN_DATA)
>      {
>          if (resultCode==RESULT_OK) {
>           String theAnswerThatIWantReturned=data.getStringExtra(????);
>        }
>      }
>  }

Perhaps. Note that requestCode is the value you supplied to
startActivityForResult(), so the code you have for that is fine, though
I'm not sure I'd rely on an Intent constant for the locally-unique value.

>  Whether the above is correct or not, I do end up in Activity B.  In
> Activity B, when I return, based on  either a menu selection or button
> click, I have no idea what to return.  I assume it will look
>  something like:
>
>      String theAnswerIWantToReturn="something";
>      Intent intent=new Intent();
>      intent.setClass(ReturnClass.this,MainClass.class);
>      startActivity(intent);

No, you need to call setResult(), then finish(). You will provide an
Intent in setResult() that will be the Intent you receive in
onActivityResult().

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com
Android App Developer Books: http://commonsware.com/books.html


-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to