Android Development notes-pass class objects through Intent, androidintent

Source: Internet
Author: User
Tags form post

Android Development notes-pass class objects through Intent, androidintent

In Android, Intent provides two methods for passing objects: one is to pass objects through the Serializable interface and the other is to pass objects through the Parcelable interface.

The objects to be passed must implement one of the above two interfaces before they can be passed directly through Intent.

Methods for passing these two objects in Intent:

Bundle. putSerializable (Key, Object); // The Bundle. putParcelable (Key, Object) Object that implements the Serializable interface; // the Object that implements the Parcelable Interface

The following uses the most common Serializable method as an example:

Assume that the object transferred from the logon interface (Login) to the main interface (MainActivity) is the User information for logon.

First, create a serialization class: User

import java.io.Serializable;public class User implements Serializable {   private int ID;    private String UserName;        private String PWD;        public final void setID(int value)    {        ID = value;    }    public final int getID()    {        return ID;    }        public final void setUserName(String value)    {        UserName = value;    }    public final String getUserName()    {        return UserName;    }      public final void setPWD(String value)    {        PWD = value;    }    public final String getPWD()    {        return PWD;    }        }

Login form post-login PASS content

Intent intent = new Intent();intent.setClass(Login.this, MainActivity.class);Bundle bundle = new Bundle();bundle.putSerializable("user", user);intent.putExtras(bundle);this.startActivity(intent);

Acceptor

Intent intent = this.getIntent(); user=(User)intent.getSerializableExtra("user");

The above can realize object transfer.

Supplement:

If List <Object> is passed, you can convert list to Serializable type, and the object type must also implement the Serializable interface.

 Intent.putExtras(key, (Serializable)list)  

Receive

(List<YourObject>)getIntent().getSerializable(key)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.