HOW TO PASS OBJECT TO ANOTHER ACTIVITY IN ANDROID?

Allen Scott

Total Post:46

Points:324
Posted by  Allen Scott
Java  OOPS 
 1098  View(s)
Ratings:
Rate this:

I have found this way to do this

Student student = new Student (18,"Zar E Ahmer");

Intent i = new Intent(this, B.class);

i.putExtra("studentObject", student);

startActivity(i);

The problem is that if the object changed in the first activity No change took place in the another activity. I thought how to make it like a constructor that no copy of the object is pass but the object it self.

  1. Brad Pitt

    Post:61

    Points:429
    Re: How to Pass object to another activity in android?

    One option could be letting your custom class implement Serializable interface and then you can pass object instances in intent extra using putExtra(Serializable..) variant of the Intent#putExtra() method.

    PSEUDO code:

    //to pass :

       intent.putExtra("MyClass", obj); 

    // to retrieve object in second Activity

    getIntent().getSerializableExtra("MyClass");

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!