Android Intent passing objects

Source: Internet
Author: User

The intent transitive class object in Android provides two ways of passing an object by implementing the Serializable interface, and one is passing an object by implementing the Parcelable interface.

The object required to be passed must implement one of the above 2 interfaces to pass directly through the intent.

Methods for passing these 2 kinds of objects in intent:

Bundle.putserializable (Key,object); Objects that implement the Serializable interface

Bundle.putparcelable (Key, Object); Objects that implement the Parcelable interface

The following are examples of the most common serializable methods:

Assume that the user information that is passed by the login interface (login) to the main interface (mainactivity) is logged in the Users class

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 to pass content after login

Intent Intent = new Intent ();

Intent.setclass (Login.this, Mainactivity.class);

Bundle bundle = new bundle ();

Bundle.putserializable ("user", user);

Intent.putextras (bundle); This.startactivity (Intent);

Receiving end

Intent Intent = This.getintent ();

user= (user) Intent.getserializableextra ("User");

The above can achieve the object delivery.

Add:

If List<object> is passed, the list can be strongly converted to the serializable type, and the Object type must also implement the Serializable interface

Intent.putextras (Key, (Serializable) list)

Receive

(list<yourobject>) Getintent (). getserializable (Key)

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.