Intent how to pass data

Source: Internet
Author: User

Method One: Use Putextra, transfer data in mainactivity, output data in secondactivity:

Package Com.dystu.intentdemo;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.view;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void Click (View view) {string[] books = new string[]{"Beauty of Programming", "sword refers to offer", "Beauty of mathematics"};intent Intent = new Intent (mainactivi Ty.this, Secondactivity.class); Intent.putextra ("Name", "Hanmeimei"); Intent.putextra ("Isgirl", true); Intent.putextra ("Age", Intent.putextra); ("Books", books); startactivity (intent);}}

Package Com.dystu.intentdemo;import Android.app.activity;import Android.os.bundle;import Android.widget.TextView; public class Secondactivity extends Activity {private String book; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_second); TextView TV = (TextView) Findviewbyid (r.id.tv); String name = Getintent (). Getstringextra ("name"); Boolean isgirl = Getintent (). Getbooleanextra ("Isgirl", false); int age = Getintent (). Getintextra ("age", 0); string[] Books = Getintent (). Getstringarrayextra ("books"); for (string string:books) {System.out.println (string);} Tv.settext ("Name:" + name + "\ n" + "Isgirl:" + isgirl + "\ n" + "Age  :" + Age);}}

You can see that the data types supported by Putextra are very limited, and Putextra cannot do this when passing some custom objects.

Mode 2: Via Serializable interface

Serializable is the serialization meaning of converting an object into a state that can be stored or transmitted, and the serialized object can be transferred on the network or stored locally.

Like what:

Package Com.dystu.intentdemo;import Java.io.serializable;public class person implements Serializable{private String name;private int age;public String getName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}

Package Com.dystu.intentdemo;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.view;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void Click (View view) {Person person = new person ();p erson.setname ("Hanmeimei");p erson.setage; Intent Intent = New Intent (Mainactivity.this, Secondactivity.class); Intent.putextra ("Person_data", person); startactivity (Intent);}}

Package Com.dystu.intentdemo;import Android.app.activity;import Android.os.bundle;import Android.widget.TextView; public class Secondactivity extends Activity {private String book; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_second); TextView TV = (TextView) Findviewbyid (r.id.tv); Person person = (person) getintent (). Getserializableextra ("Person_data"); Tv.settext ("Name:" + person.getname () + "\ n" + "Age:" + person.getage ());}}


Data can be removed from the Getserializableextra.

Mode 3: Via Parcelable interface

Different objects are serialized, and the Parcelable method is implemented by decomposing a complete object, and each part of the decomposition is the type supported by intent.

As follows:

Package Com.dystu.intentdemo;import Android.os.parcel;import Android.os.parcelable;public class Person implements Parcelable{private string name;private int age;public string getName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} @Overridepublic int describecontents () {return 0;} @Overridepublic void Writetoparcel (Parcel dest, int flags) {dest.writestring (name);d Est.writeint (age); public static final parcelable.creator<person> Creator = new creator<person> () {@Overridepublic person[] NewArray (int size) {return new person[size];} @Overridepublic person Createfromparcel (Parcel source) {Person person = new person ();p erson.name = source.readstring (); Person.age = Source.readint (); return person;}};}

The mainactivity code does not change.

Package Com.dystu.intentdemo;import Android.app.activity;import Android.os.bundle;import Android.widget.TextView; public class Secondactivity extends Activity {private String book; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_second); TextView TV = (TextView) Findviewbyid (r.id.tv); Person person = (person) getintent (). Getparcelableextra ("Person_data"); Tv.settext ("Name:" + person.getname () + "\ n" + " Age: "+ person.getage ());}}


Parcelable efficiency is a bit higher.

Intent how 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.