Intent ways to pass data

Source: Internet
Author: User

First, pass the list
1. Ways to pass List<string>
arraylist<string> info = new arraylist<string> ();
Info.add (Weibo);
Intent Intent = new Intent (mainactivity.this, Resultactivity.class);
Intent.putstringarraylistextra ("infolist", info);
StartActivity (Intent);

Receive
arraylist<string> infolist = new arraylist<string> ();
Infolist = Getintent (). Getstringarraylistextra ("Infolist");

2. Ways to pass List<integer>
Intent.putintegerarraylistextra (key, list);
List = (arraylist<integer>) getintent (). Getintegerarraylistextra (key);

Two., passing object

1. Using the Serializable method
Premise: Object needs to implement serializable interface
The syntax for passing object in serializable mode: bundle.putserializable (Key,object);
The syntax for receiving object in serializable mode: object= (object) getintent (). Getserializableextra (key);
The implementation of the serializable interface is to serialize the object, and then transfer, and Java's common programming is no obvious difference, and object does not need to change significantly, recommended in this way.

Serinfo serinfo = new Serinfo (name, website, Weibo);
Intent Intent = new Intent ();
Bundle bundle = new bundle ();
Bundle.putserializable ("Serinfo", serinfo);
Intent.setclass (Mainactivity.this, Resultactivity.class);
Intent.putextras (bundle);
StartActivity (Intent);
Get the value of the serializable method passed over
Serinfo Serinfo = (serinfo) getintent (). Getserializableextra ("Serinfo");

2. Using the Parcelable method
Premise: Object needs to implement Parcelable interface
The syntax for passing object in parcelable mode: bundle.putparcelable (Key,object);
The syntax for receiving object in parcelable mode: object= (object) getintent (). Getparcelableextra (key);
The class that implements the Parcelable interface is more complex, what parcelable is a thing?

Iii. Transmission of List<object>

arraylist<serinfo> listobj = new arraylist<serinfo> ();
Serinfo serInfo1 = new Serinfo (name, website, Weibo);
Serinfo SerInfo2 = new Serinfo (name, website, Weibo);
Listobj.add (SERINFO1);
Listobj.add (SerInfo2);
Intent Intent = new Intent ();
Intent.setclass (Mainactivity.this, Resultactivity.class);
Intent.putextra ("Listobj", (Serializable) listobj);
StartActivity (Intent);

Get the list<object>.
Arraylist<serinfo> listobj = (arraylist<serinfo>) getintent (). Getserializableextra ("LISTOBJ");

Intent ways to pass data

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.