Learn Android with Java and Kotlin
 

Pass custom object from one activity to another

If you have just started learning to create android apps, probably you are playing a lot around activities . Creating new activities , starting one activity from another , passing values between activities etc . Well, have you ever tried sending your custom java objects to an activity ? The way you did it for an int or string does not work here , right! In this tutorial you’ll learn how to do that in the following steps .

Steps:

  • 1.Create your java bean class.
  • 2.Make your class implement Parcelable.
  • 3.Use putExtra(String name, Parcelable value) to add it to the intent in ActivityA.
  • 4.Get your custom object in ActivityB using intent.getParcelableExtra(String nam)

1.Create your java bean class.
Let us consider a simple class User

public class User {
    private String mName;
    private String mEmail;

    public String getmName() {
        return mName;
    }

    public void setmName(String mName) {
        this.mName = mName;
    }

    public String getmEmail() {
        return mEmail;
    }

    public void setmEmail(String mEmail) {
        this.mEmail = mEmail;
    }
}

2.Make your class implement Parcelable.

public class User implements Parcelable{
    private String mName;
    private String mEmail;

    protected User(Parcel in) {
        mName = in.readString();
        mEmail = in.readString();
    }

    public static final Creator<User> CREATOR = new Creator<User>() {
        @Override
        public User createFromParcel(Parcel in) {
            return new User(in);
        }

        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };

    public String getmName() {
        return mName;
    }

    public void setmName(String mName) {
        this.mName = mName;
    }

    public String getmEmail() {
        return mEmail;
    }

    public void setmEmail(String mEmail) {
        this.mEmail = mEmail;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mName);
        dest.writeString(mEmail);
    }
}

3.Use putExtra(String name, Parcelable value) to add it to the intent in ActivityA.
Now we can use intent.putExtra(“user”,user) as User implements Parcelable.

public class ActivityUserForm extends AppCompatActivity {

    private EditText mEtName;
    private EditText mEtEmail;
    private Button mBtnSubmit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*Initialize the UI elements*/
        mBtnSubmit= (Button) findViewById(R.id.btn_submit);
        mEtEmail= (EditText) findViewById(R.id.et_email);
        mEtName= (EditText) findViewById(R.id.et_name);

        /*add action to the button*/
        mBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                /*
                * Get the user inputs fromthe EditTexts
                * */
                String name=mEtName.getText().toString();
                String email=mEtEmail.getText().toString();

                /*
                * Create the user object
                * */
                User user=new User();
                user.setmName(name);
                user.setmEmail(email);

                /*
                * Create the Intent to start ActivityUserDetails
                * */
                Intent intent=new Intent(ActivityUserForm.this,ActivityUserDetails.class);
                intent.putExtra("user",user);
                startActivity(intent);

            }
        });

    }
}

4.Get your custom object in ActivityB using intent.getParcelableExtra(String nam)
To retrieve the user object from intent in the receiving activity we shall use getIntent().getParcelableExtra(“user”);

public class ActivityUserDetails extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_details);

        /*Get the User parcel from the Intent*/
        User user=getIntent().getParcelableExtra("user");

        TextView tvName= (TextView) findViewById(R.id.tv_name);
        TextView tvEmail= (TextView) findViewById(R.id.tv_email);

        /*Set the values*/
        tvName.setText(user.getmName());
        tvEmail.setText(user.getmEmail());
    }
}

Leave a Reply

Your email address will not be published.