ANDROID_ Social software 2_ global variables

Source: Internet
Author: User

1. Use Application to save global variable amount

There's not much theoretical stuff here, it's just some practical operation.

1.1 Defining the Data class inheritance application Data.class

android. App. Application;public class Data extends Application{private string b;public string Getb () {return this.b;} public void Setb (String c) {this.b= C;} @Overridepublic void OnCreate () {b = "Hello"; Super.oncreate ();}}

1.2 Declaration in Manifest.xml application

android: name= "Com.animator1.Data" >

1.3 Creation of two activity

Mainactivity.class

public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Button btn = (button) This.findviewbyid (R.id.button1), final TextView Txtview = (TextView) This.findviewbyid (R.id.main); Final Data app = (data) getapplication (); Txtview.settext (App.getb ()); Btn.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubapp.setb ("No_hello"); Txtview.settext ( APP.GETB ()); Intent Intent = new Intent (mainactivity.this, Secondactivity.class); startactivity (Intent);}});}}

Secondactivity.class

public class Secondactivity extends activity{@Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.second); Button btn = (button) This.findviewbyid (R.id.button2), final TextView Txtview = (TextView) This.findviewbyid (r.id.second ), final Data app = (data) getapplication (); Txtview.settext (App.getb ()); Btn.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubapp.setb ("Hello"); Txtview.settext (App.getb ()); Intent Intent = new Intent (secondactivity.this, Mainactivity.class); StartActivity ( Intent);}});}}

1.4 Running the program

Note: The program first enters Mainactivity.class, obtains the value of variable b in data, displays the interface hello shown in Figure 1, and App.setb ("No_hello") after clicking the button to modify the value of variable B.

Jump to Secondactivity.class, get the value of variable b in data, display the interface shown in Figure 2 No_hello, click the button App.setb ("Hello"), modify the value of variable B.

Jump back to Mainactivity.class ... Cycle

2. Use normal class Data.class to save global variable

The method is similar to the above.

1.1 Definition Data.class

public class Data{private static string a = "Feiyangxiaomi";p ublic static string Geta () {return A;} public static void SetA (String a) {data.a = A;}}

1.2 Creation of two activity

Mainactivity.class

public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Button btn = (button) This.findviewbyid (R.id.button1), final TextView Txtview = (TextView) This.findviewbyid (R.id.main); Txtview.settext (Data.geta ()); Btn.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View V {//TODO auto-generated method Stubdata.seta ("No_feiyangxiaomi"); Intent Intent = new Intent (Mainactivity.this, Secondactivity.class); startactivity (intent);}});}

Secondactivity.class

public class Secondactivity extends activity{@Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.second); Button btn = (button) This.findviewbyid (R.id.button2), final TextView Txtview = (TextView) This.findviewbyid (r.id.second ); Txtview.settext (Data.geta ()); Btn.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick ( View v) {//TODO auto-generated method Stubdata.seta ("Feiyangxiaomi"); Intent Intent = new Intent (Secondactivity.this, Mai Nactivity.class); startactivity (intent);}});}

1.3 Running the program

Note: The program first enters Mainactivity.class, gets the value of variable b in data, displays the interface Feiyangxiaomi shown in Figure 1, and after clicking Button App.setb ("No_feiyangxiaomi"), modify the value of variable B.

Jump to Secondactivity.class, get the value of variable b in data, display the interface shown in Figure 2 No_feiyangxiaomi, click the button App.setb ("Feiyangxiaomi"), modify the value of variable B.

Jump back to Mainactivity.class ... Cycle

3. Summary
Both of these methods I can test, most people should be more inclined to the first, after all, application is an application portal specifically provided by Android , and the cycle runs throughout the program.

From

http://blog.csdn.net/feiyangxiaomi/article/details/9966215

Android_ Social software 2_ global variables

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.