-4

Can I pass by reference object between activity in Android , How ?

Thankyou

  • 2
    I'm voting to close this question as off-topic because the OP is a **lazybone**. No effort is shown in googling for `android pass object by reference between activities` – Phantômaxx Jul 28 '16 at 18:56
  • 1
    yes, if your object is a custom `Binder` class, but it is a risky stuff, as it its a "live" object and your sending activity can be dead in any time – pskink Jul 28 '16 at 19:09
  • Possible duplicate of [How to pass an object from one activity to another on Android](http://stackoverflow.com/questions/2736389/how-to-pass-an-object-from-one-activity-to-another-on-android) – Dinidu Hewage Jul 29 '16 at 00:12

2 Answers2

2

You should have googled it.

This, this, this or this

You can use these examples if your objects implements Parcelable or Serializable. Unfortunaley it is not possible to send an object be reference. You can get around this problem be sending te object back:

returning the object:

Intent result = new Intent();
result.putExtra("NAME", myEditedObject);
setIntent(result);
setResult(Activity.RESULT_OK);
finish();

reading the returned object:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == MY_REQUEST_CODE) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            Bundle b = this.getIntent().getExtras();
            if (b != null)
                myObject= (MyObject) b.getParcelable(STRING_NAME);
        }
    }
}
Community
  • 1
  • 1
Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116
0

If your object implements Parcelable or Serializable, then the answer is yes. Just put the object in the intent that you're going to use to display the activity.

Lino
  • 5,084
  • 3
  • 21
  • 39