Several ways to transfer values of activity

Source: Internet
Author: User
Tags key string

Transfer value of activity

* The first type: Intent Pass Value

The Settings button in the main XML file, click Jump: <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:text = "@string/btn_name" android:onclick= "send"/>//Set Click Jump event in main Java code to pass value: public class Mainactivity extends Activity {@        Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }//Click button to pass data to specified activity in public void Send (view view) {Intent Intent = new Intent (Mainactivity.this, Otheracti        Vity.class);        Intent.putextra ("name", "Zhang San");//putextra (String key,value): Key indicates the unique value that is to be transmitted, value is the data Intent.putextra ("age", 20);        Intent.putextra ("Sex", ' male ');        Intent.putextra ("BL", true);    StartActivity (Intent); }} to add TextView to the XML layout of the target activity to display the obtained value, get the value in the Java code of the target activity: protected void OnCreate (Bundle savedinstancestate) {//TODO    auto-generated method Stub super.oncreate (savedinstancestate);    Setcontentview (R.layout.activity_other); TV = (TextView) Findviewbyid (r.id.tv);    1. Get the Intent object that activates the component Intent Intent = Getintent ();    2. Obtain the passed data according to key String name = Intent.getstringextra ("name"); int age = Intent.getintextra ("Age", 0);//getintextra (the key that gets the data, if the default value is displayed if the data is not obtained according to key) char sex = Intent.getcharextra ("    Sex ",");    Boolean bl = Intent.getbooleanextra ("BL", false);    3. Display the data in TextView tv.settext ("Name:" +name+ "Age:" +age+ "Sex:" +sex+ "BL:" +BL "); }

* Second: Bundle pass value

  public void Send (view view) {//1. Create Intent Intent object Intent Intent = new Intent (Mainactivity.this, otheractivity    . Class);    2. Create a bundle object to store the data bundle bundle = new Bundle () that needs to be passed;    3. Store the data that needs to be passed into the bundle object Bundle.putstring ("name", "Sister Feng");    Bundle.putchar ("Sex", ' female ');    Bundle.putint ("Age", 20);    4. Store the bundle object in the intent object Intent.putextras (bundle); 5. Activate activity startactivity (intent);} protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated Method stub super.oncreate (savedinstance    State);    Setcontentview (R.layout.activity_other);    TV = (TextView) Findviewbyid (r.id.tv);    1. Get the Intent object that activates the component Intent Intent = Getintent ();    2. Get the bundle of objects that are passed bundle bundle = Intent.getextras ();    3. Gets the data passed in the bundle based on key String name = bundle.getstring ("name");    int age = Bundle.getint ("Age");    char sex = Bundle.getchar ("Sex"); 4. Display the data to TextView tv.settext ("Name:" +name+ "Age:" +age+ "Sex:" +sex);}  

* Third: Use application global object to pass value

    1. Create a separate class to inherit application, you will need to store the data definition, and provide the set and get method 2. Stores data in the sent Activity 3. Get data in the received Activity 4. Register in the manifest file &l  T;application android:name= "Custom application package name. Class Name" > </application>public class MyApplication extends    Application {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;    }}public class Mainactivity extends Activity {private MyApplication application;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.activity_main);        }//Click the button to pass the data to the target activity in public void Send (view view) {application = (MyApplication) getapplication ();        Application.setname ("Xiao Ming");        Application.setage (18); StartActivity (New Intent (MainACtivity.this, Otheractivity.class));    }}public class Otheractivity extends Activity {private TextView TV;    Private MyApplication Application; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub super.o        Ncreate (savedinstancestate);        Setcontentview (R.layout.activity_other);        TV = (TextView) Findviewbyid (r.id.tv);        Application = (MyApplication) getapplication ();        String name = Application.getname ();        int age = Application.getage ();    Tv.settext ("name=" +name + "age=" +age); }}<application android:name= "Com.example.creatactivity.MyApplication" ></application>

* Fourth: Activation of activity callback data

案例演示:

public class Mainactivity extends Activity {private EditText et_num1,et_num2;    Private TextView Tv_result;    private static final int request_code=1;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        ET_NUM1 = (EditText) Findviewbyid (R.ID.EDITTEXT1);        et_num2 = (EditText) Findviewbyid (R.ID.EDITTEXT2);    Tv_result = (TextView) Findviewbyid (R.ID.TEXTVIEW3); }//Click button to pass data to target activity in public void OnClick (view view) {Intent Intent = new Intent (Mainactivity.this, Othera        Ctivity.class);        String NUM1 = Et_num1.gettext (). toString ();        String num2 = Et_num2.gettext (). toString ();        Intent.putextra ("Num1", NUM1);        Intent.putextra ("num2", num2); Startactivityforresult (Intent, Request_code);//startactivityforresult (requested Intent object, integer request code greater than 0)}/* * This method is used to handle set Result () method callback data * int Requestcode, request code * int resultCode, result code * Intent data, Callback Intent Intent Object */@Override protected void Onactivityresult (int requestcode, int res Ultcode, Intent data) {//TODO auto-generated Method Stub Super.onactivityresult (Requestcode, ResultCode, D        ATA); if (Requestcode = = REQUEST_CODE&AMP;&AMP;RESULTCODE==ACTIVITY.RESULT_OK) {String RESULT = Data.getstringextra ("I            NFO ");        Tv_result.settext (result);    }}}public class Otheractivity extends Activity {private TextView tv_num1,tv_num2;    Private EditText Et_result; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub super.o        Ncreate (savedinstancestate);        Setcontentview (R.layout.activity_other);        TV_NUM1 = (TextView) Findviewbyid (R.ID.TV1);        tv_num2 = (TextView) Findviewbyid (R.ID.TV3);        Et_result = (EditText) Findviewbyid (R.ID.ET1);        Intent Intent = Getintent (); Tv_num1.settext (Intent.getstringextra ("NUM1"));    Tv_num2.settext (Intent.getstringextra ("num2"));                }//Click the button to upload the data back to the main activity public void Send (view view) {String result = Et_result.gettext (). toString ();//Get result data        Intent Intent = new Intent ();                Intent.putextra ("info", result); Setresult (ACTIVITY.RESULT_OK, intent);//setresult (represents the requested result code, which represents the intent object of the postback data) OtherActivity.this.finish ();// Close the Current Activity}}

Several ways to transfer values from an activity

Related Article

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.