Intent four kinds of data transfer methods Mainactivity value operation
public class Mainactivity extends Appcompatactivity {/** * This method is called when an activity is created, and we can do the initialization of the component in that method * @param Savedinstancestate */@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (save Dinstancestate); Setcontentview (R.layout.activity_main); System.out.println ("Mainactivity-oncreate"); /** * Call this method when activity is visible but not yet interactive * * * after oncreate @Override protected void OnStart () {su Per.onstart (); System.out.println ("Mainactivity-onstart"); }/** * Called when activity can interact, called after OnStart, after the method is called, the activity enters the active state * resumed status */@Override Protec ted void Onresume () {super.onresume (); System.out.println ("Mainactivity-onresume"); /** * An activity from the stoped state is reactivated when called * and then the OnStart () method is called */@Override protected void Onrestart () { Super.onrestart (); System.out.println ("Mainactivity-onrestart"); }/** * Current ACTIVity is overwritten by another activity, loses focus, but also visible, cannot interact * then the current activity enters the paused state (paused state) * The Onresume () method is called again when the activity is re-acquiring focus in this state */ @Override protected void OnPause () {super.onpause (); System.out.println ("Mainactivity-onpause"); }/** * Current activity is completely overwritten by another activity, invisible and non-interactive * Then the current activity enters the stoped state (stop state) * When activity in this state is re-displayed, it is called onre Start () method */@Override protected void OnStop () {super.onstop (); System.out.println ("Mainactivity-onstop"); /** * Called when the current activity is destroyed, indicating the end life cycle */@Override protected void OnDestroy () {Super.ondestroy (); System.out.println ("Mainactivity-ondestroy"); public void Activityclick (View v) {//Opens another activity, a Intent object represents an intent Intent Intent = new Intent (This, Main2activity.class); Assign value to activity one: Create a Bundle object to encapsulate the data bundle with the new bundle (); Data.putint ("id", 1); Data.putstring ("name", "macro"); Data.putfloat ("Age", 20.5f); Intent.putextra ("Data", data); Transfer value to activity two: Bundle object prepared with intent//Intent.putextra ("id", 1);//Intent.putextra ("name", "Big Thunder");//INT Ent.putextra ("Age", 18.5f); StartActivity (intent);//Start Activity}/** * Method Three: Pass Object data * @param view */public void Sendobjectclick (view View) {Intent Intent = new Intent (this,main2activity.class); User user = new user (); User.setid (1); User.setname ("macro"); User.setsex ("male"); Intent.putextra ("user", user);//user object must implement the serialization interface StartActivity (intent); }/** * Mode four: Pass object with parcelable * @param view */public void Parcelableclick (view view) {Dog dog = new Dog (); Dog.setname ("Tom"); Dog.setsex ("public"); Dog.setage (5); Intent Intent = new Intent (this,main2activity.class); Intent.putextra ("Dog", dog); StartActivity (Intent); }}
Main2activity the corresponding operation of receiving data
public class Main2activity extends Appcompatactivity {private TextView Textview2_data,textview2_user,textview2_parcel Able_dog; /** * This method is called when an activity is created, through which we can initialize the component in this method * @param savedinstancestate */@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main2); System.out.println ("Main2activity-oncreate"); Textview2_data = (TextView) Findviewbyid (r.id.textview2_data); Textview2_user = (TextView) Findviewbyid (R.id.textview2_user); Textview2_parcelable_dog = (TextView) Findviewbyid (R.id.textview2_parcelable_dog); Intent Intent = Getintent (); System.out.println ("2-" +intent); Mode one value//Bundle data = Intent.getbundleextra ("data");//int id = data.getint ("id");//String name = Data.getstring ("name");//float = data.getfloat ("Age"); Mode two values//int id = Intent.getintextra ("id", 0);//String name = Intent.getstringextra ("name");//Float age = Intent.getfloatextra ("Age", 18f);//// Textview2_data.settext ("id=" +id+ ", name=" +name+ ", age=" +age "); Mode three values//user user = (user) Intent.getserializableextra ("user");//Textview2_user.settext (user.tostring ()); The way four takes the value of dog dog = Intent.getparcelableextra ("Dog"); Textview2_parcelable_dog.settext (Dog.tostring ()); /** * Call this method when activity is visible but not yet interactive * * * after oncreate @Override protected void OnStart () {su Per.onstart (); System.out.println ("Main2activity-onstart"); }/** * Called when activity can interact, called after OnStart, after the method is called, the activity enters the active state * resumed status */@Override Protec ted void Onresume () {super.onresume (); System.out.println ("Main2activity-onresume"); /** * An activity from the stopped state is reactivated when called * and then the OnStart () method is called */@Override protected void Onrestart () { Super.onrestart (); System.out.println ("Main2activity-onrestart"); }/** * Current activity is overwritten by another activity, loses focus, but also visible, cannot interact * then the current activity enters the paused state (paused state) * When the activity re-acquires the focus in this state Using the Onresume () method */@Override protected void OnPause () {super.onpause (); System.out.println ("Main2activity-onpause"); }/** * Current activity is completely overwritten by another activity, invisible and non-interactive * Then the current activity enters the stopped state (stop state) * When activity in this state is re-displayed, it is called ONR Estart () method */@Override protected void OnStop () {super.onstop (); System.out.println ("Main2activity-onstop"); /** * Called when the current activity is destroyed, indicating the end life cycle */@Override protected void OnDestroy () {Super.ondestroy (); System.out.println ("Main2activity-ondestroy"); public void openactivity (View v) {//Opens another activity, a Intent object represents an intent Intent Intent = new Intent (this,main Activity.class); StartActivity (intent);//Start Activity}}
Parcelable object Preparation When passing a value:
public class Dog implements parcelable{private String name; Private String sex; private int age; Object Creator (We aim to remove the data from the package and encapsulate it as an object (DOG)) public static final parcelable.creator<dog> Creator = new Parcelable.crea Tor<dog> () {//Remove the data from the package @Override public Dog createfromparcel (Parcel source) {Syst Em.out.println ("Createfromparcel"); Dog dog = new Dog (); Dog.setname (Source.readstring ()); Dog.setsex (Source.readstring ()); Dog.setage (Source.readint ()); return dog; } @Override Public dog[] NewArray (int size) {return new dog[size]; } }; public string GetName () {return name,} public void SetName (String name) {this.name = name,} public string Getsex () {R Eturn sex; public void Setsex (String sex) {this.sex = sex,} public int getage () {return age;} public void Setage (int age) {Thi S.age = age; } @Override Public String toString () {return ' dog{' + ' name= ' + name + ' \ ' + ', sex= ' + sex + ' \ ' + ', age= ' + Age + ‘}‘; } }
Intent four ways of data transfer