Android--Activity value delivery

Source: Internet
Author: User

We need to create 2 activity, one is a and the other is B.

Simple value-passing

The activity organization of a is going to transfer the data to B.
Intent i = new Intent (amainactivity.this,bmainactivity.class); This line of code creates a Intent object, which is then used to transfer data.

 PackageCom.example.testapp;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.view.Menu;ImportAndroid.view.View; Public  class amainactivity extends Activity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_amain); Findviewbyid (R.id.button1). Setonclicklistener (NewView.onclicklistener () {@Override                     Public void OnClick(View v) {Intent i =NewIntent (amainactivity. This, Bmainactivity.class);//Pass ParametersI.putextra ("Name","Gaopeng");                    StartActivity (i);    }                }); }@Override     Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.amain, menu);return true; }}

The activity of B receives data from a and obtains the value by I.getstringextra ("name").

 PackageCom.example.testapp;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.widget.TextView; Public  class bmainactivity extends Activity {    PrivateTextView T;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_bmain);        t = (TextView) Findviewbyid (r.id.tv);        Intent i = getintent (); T.settext (I.getstringextra ("Name")); }@Override     Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.bmain, menu);return true; }}
Data Packet Bundle

The activity of a, we use the bundle class in the code, a bit like the usage of the map collection in our java.
Use the Putextras or Putextra method to put the packet into the transport object. Two method differences, one is placed directly, and the other is a packet alias, similar to the storage format kv.

 PackageCom.example.testapp;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.view.Menu;ImportAndroid.view.View; Public  class amainactivity extends Activity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_amain); Findviewbyid (R.id.button1). Setonclicklistener (NewView.onclicklistener () {@Override                     Public void OnClick(View v) {Intent i =NewIntent (amainactivity. This, Bmainactivity.class); Bundle B =NewBundle (); B.putstring ("Name","Gaopeng"); B.putint ("Age", -);//I.putextras (b);I.putextra ("User", b);                    StartActivity (i);    }                }); }@Override     Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.amain, menu);return true; }}

Activity of B, through the Getbundleextra or Getextras method channel bundle object, and then according to the storage time name, take out the value.

 PackageCom.example.testapp;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.widget.TextView; Public  class bmainactivity extends Activity {    PrivateTextView T;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_bmain);        t = (TextView) Findviewbyid (r.id.tv);        Intent i = getintent (); Bundle B = I.getbundleextra ("User");//Bundle B = I.getextras ();T.settext ("Name="+ b.getstring ("Name") +", age="+ B.getint ("Age")); }@Override     Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.bmain, menu);return true; }}
Transfer Object

Since it is a transfer object, we need to create an object class.
The object class needs to implement the serialization interface, here I implement the Serializable interface, Android has its own serialization interface Parcelable, next blog, I detail the differences and usage.

 PackageCom.example.testapp;Importjava.io.Serializable;ImportAndroid.os.Parcel;Importandroid.os.Parcelable; Public  class User implements Serializable {    //public class User implements parcelable{    PrivateString name;Private intAge PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; } User (String name,intAge) { This. name = name; This. Age = Age; }}

Activity of A,

 PackageCom.example.testapp;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.view.Menu;ImportAndroid.view.View; Public  class amainactivity extends Activity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_amain); Findviewbyid (R.id.button1). Setonclicklistener (NewView.onclicklistener () {@Override                     Public void OnClick(View v) {Intent i =NewIntent (amainactivity. This, Bmainactivity.class); I.putextra ("User",NewUser ("Lijingwei", -));                    StartActivity (i);    }                }); }@Override     Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.amain, menu);return true; }}

Activity of B, gets the object through Getserializableextra. This approach should often be used in our future development. Conforms to our Java object-oriented development.

 PackageCom.example.testapp;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.widget.TextView; Public  class bmainactivity extends Activity {    PrivateTextView T;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_bmain);        t = (TextView) Findviewbyid (r.id.tv);        Intent i = getintent (); User user = (user) I.getserializableextra ("User"); T.settext ("Name="+ user.getname () +", age="+ user.getage ()); }@Override     Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.bmain, menu);return true; }}
Attached to the run-time

Activity interface of a

Activity interface of B

Android--Activity value delivery

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.