0

i want to learn how to send message data from 1 activity to another and receive the data in onActivityResult

i am not sure if this is the right way but hope you can show me the right way.

Mainactivity on button1 click

Intent returnIntent = new Intent();
returnIntent.putExtra("demo1");
setResult(RESULT_OK,returnIntent);

on button2 click

Intent returnIntent = new Intent();
returnIntent.putExtra("sarah22");
setResult(RESULT_OK,returnIntent);

Mainactivity2

protected void onActivityResult(int requestCode, int resultCode, Intent data) {


            if ("demo1") {

                Context context = getApplicationContext();
                CharSequence text = "button1 demo1 received";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();

            }



if ("sarah22") {

                Context context = getApplicationContext();
                CharSequence text = "Button2 sarah22 received";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();

            }
        }
    }
Jean Lee
  • 67
  • 2
  • 7
  • Is your code not working? – Soham Jun 23 '15 at 19:53
  • no sir as i am new i am trying to figure out how this is done, i am a visual basic 6 and .net developer and now trying to learn about this its fun kindly show me an example soham will be greatfull to learn from your example basic cheers. – Jean Lee Jun 23 '15 at 19:59
  • possible duplicate of [How to manage start activity for result on Android?](http://stackoverflow.com/questions/10407159/how-to-manage-start-activity-for-result-on-android) – Soham Jun 23 '15 at 20:16

2 Answers2

0

I just wanted to clarify my self again. I wish to use something like this:

Intent returnIntent = new Intent();
returnIntent.putExtra("demo1");
setResult(RESULT_OK,returnIntent);

or maybe

Intent intent = new Intent(MainActivity.this, New2.class);
                intent.putExtra("demo1", "demo1");
                startActivity(intent);

where you see demo1 you normally will put a requestcode there but how do I send this off to the activity2 and from their

in activity2

if ("demo1") {

                Context context = getApplicationContext();
                CharSequence text = "button1 demo1 received";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();

            }

where you see if ("demo1") { if this string is found then show toast

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jean Lee
  • 67
  • 2
  • 7
  • This is not an answer. You should use the [edit] button and add this to the question if it is part of the question, otherwise people will think it is an answer and you will get no more answers..... – Brian Tompsett - 汤莱恩 Jun 24 '15 at 10:50
0

You should to this:

          Intent intent = new Intent(getBaseContext(), Second.class);
          intent.putExtra("ID", "I love stackoverflow");
          startActivityForResult(intent)

In the second activity you can grab the data,

         String id = intent.getStringExtra("ID");

and do what you want with it. To return to the first activity please use setResult(OK) and call finish.

  Intent returnIntent = new Intent();
  setResult(RESULT_CANCELED, returnIntent);
  finish();

in this case the first activity onActivityResult() going to fire and you can handler the returned value there.

sources: Using intents to pass data between activities in android

http://developer.android.com/training/basics/intents/result.html

How to manage `startActivityForResult` on Android?

Community
  • 1
  • 1
narancs
  • 5,234
  • 4
  • 41
  • 60