1

I want to send user define object from one activity to another in android application.

I have created user class object and send this user object to my second activity from first activity.

Chetan Chaudhari
  • 323
  • 5
  • 15

3 Answers3

1

Implement your class with Serializable interface. Then pass the object using

intent.putExtra("MyClass", obj);

and retrieve object by calling

getIntent().getSerializableExtra("MyClass");

See this post

Community
  • 1
  • 1
pz64_
  • 2,212
  • 2
  • 20
  • 43
1
  1. Make sure your User class implements Parcelable.

    public class User implements Parcelable {
    
        ...........
        ............... 
    }
    
  2. Send User object to SecondActivity as below:

    User userObject = new User();
    
    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    intent.putExtra("user_data", userObject);
    startActivity(intent);
    
  3. Retrieving the User object in SecondActivity.

    User user = (User) getIntent().getParcelableExtra("user_data");
    

Here is good Tutorial about using Parcelable.

Hope this will help~

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
0

Iplemented my user define class to Parcelable interface.

Chetan Chaudhari
  • 323
  • 5
  • 15