Two ways to value activity in Android

Source: Internet
Author: User

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

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.