Alfred James
Alfred James

Reputation: 1019

Can startActivityForResult() send data?

I am learning android and curious to know as if startActivityForResult() contains the properties of startActivity() too, i.e. can it be used to send data like startActivity() beside receiving data from called activity?

Here is the code:
SendData Activity:

Intent data= new Intent(SendData.this, RecieveData.class);
Bundle check = new Bundle();

check.putString("UmerData", cheese);
medt.setText(cheese);
data.putExtras(check);
startActivityForResult(data, 5);

Should receive data in this activity (RecieveData Activity)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recievedata);
    Initializek();
    Bundle got = getIntent().getExtras();
    String ss= got.getString("UmerData");
    if(getIntent()!=null && ss!=null ) {
        rt1.setText("Is not Null");
    }
}

Your help will be really appreciated !

Thanks

Upvotes: 2

Views: 7493

Answers (3)

Andrew
Andrew

Reputation: 122

I'm working in Xamarin Android so the code is C# but I had the same problem except I'm sending between separate apps. I eventually got it working so here is my example.

var intent = new Intent();
intent.SetComponent(new ComponentName("com.company.packageName", "com.company.packageName.activityName"));
intent.PutExtra(Intent.ExtraText, message);
StartActivityForResult(intent, 1);
// in app being started
protected override void OnResume()
{
    base.OnResume();

    Intent intent = Intent; // Equivalent to getIntent()
    if (intent != null)
    {
           string str = intent.GetStringExtra(Intent.ExtraText);
           if (str != null)
           {
              // Do stuff with str
           }
           else
           {
               //Show Error
           }  
         }
     else
     {
              //Show Error
     }
}

Upvotes: 0

telkins
telkins

Reputation: 10550

When you use startActivityForResult(), you have to also create a onActivityResult() method within the Activity that called startActivityForResult(). onActivityResult() is where you can access the Intent stored by the Activity you start for a result.

In the Activity that is then started, you have to call setResult() to store the Intent that you store the data in.

Read over this: http://developer.android.com/reference/android/app/Activity.html#StartingActivities

edit: Misread your question. For passing a Bundle of options through, I would use this overload:

startActivity(Intent, int, Bundle)

Upvotes: 0

Chitranshu Asthana
Chitranshu Asthana

Reputation: 1089

Yes, startActivity & startActivityForResult, both take intent as param. You can bundle launch data inside intent and pass it over to target activity.

Upvotes: 0

Related Questions