First: The first activity
/** * Jump to Activity2 interface by this method */public void GotoActivity2 (View v) {//Create an Intent Intent intent=new Intent (This,mainac Tivity2.class); The first method of transmitting value bundle bundle=new bundle (); Bundle.putstring ("name", "Zhangsan"); Bundle.putint ("Age", 23); Intent.putextra ("person", bundle); Start another activity startactivity (intent); }
A second activity
/** * activity is created when call * can initialize the UI component in this method * the method call is complete calls the OnStart () method * */ @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedInstanceState); setcontentview (r.layout.activity_main2); system.out.println ("Mainactivity2-oncreate ()"); //gets the parameter intent intent=getintent () that was passed from the previous activity; bundle bundle=intent.getbundleextra ("person"); string name= bundle.getstring ("name"); int age=bundle.getint ("Age"); system.out.println (name+ " : "+age"; textview textview= (TextView) findviewbyid (R.ID.TEXTVIEW2); textview.settext ("Name=" + Name+ " age=" +age); }
The second type: activity1
The second way of transmitting value is Intent.putextra ("name", "small white"); StartActivity (Intent);
Activity2
Intent intent=getintent (); String name2= Intent.getstringextra ("name"); TextView textview= (TextView) Findviewbyid (R.ID.TEXTVIEW2); Textview.settext ("name2=" +name2);
Passing Custom types (custom classes, custom classes must be serialized)
Activity1
Pass the custom type cat cat=new cat (1, "Beauty queen", 23); Intent.putextra ("Cat", cat); StartActivity (Intent);
Activity2
The second type of String name2= Intent.getstringextra ("name"); Cat cat= (CAT) Intent.getserializableextra ("Cat"); TextView textview= (TextView) Findviewbyid (R.ID.TEXTVIEW2); Textview.settext ("cat=" +cat.tostring ());
This article is from the "matengbing" blog, make sure to keep this source http://matengbing.blog.51cto.com/11395502/1880953
Two ways to value activity in Android