Android uses global variables to pass data

Source: Internet
Author: User

Using global variables to pass data, so-called global variables are similar to application variables in JEE development. Declared after the global call. The memory is destroyed only after it has been cleaned. Otherwise it can always be called.

Or use the click of a button to pass one data to another page to display.

First, you create a new project, and then you open the Mainactivity.xml file, and then you add a button.

<button        android:id="@+id/button"        android:layout_width= " wrap_content "         android:layout_height="wrap_content"        Android:text =" pass data with application " />

Create a new Myapp.java class to store global variables

 PublicString name; @Override Public voidonCreate () {//TODO auto-generated Method Stubsuper.oncreate (); SetName ("Hello"); }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicMyApp () {//TODO auto-generated Constructor stub}

Then, in Mainactivity.java, add

Privatebutton button; PrivateMyApp MyApp; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button= (Button) This. Findviewbyid (R.id.button); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub//get global variable datamyapp=(MYAPP) getapplication (); //Myapp.setname ("World");//can be re-assigned valueIntent Intent =NewIntent (mainactivity. This, Otheractivity.class);            StartActivity (Intent);    }        }); }

New layout file Other.xml file join

<textview android:id="@+id/msg" android:layout_width="fill_parent  "    android:layout_height="fill_parent"/>

New Otheractivity.java File

 Public classOtheractivity extends Activity {PrivateMyApp MyApp; PrivateTextView TextView;  Publicotheractivity () {//TODO auto-generated Constructor stub} @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stubsuper.oncreate (savedinstancestate);        Setcontentview (R.layout.other); TextView= (TextView) This. Findviewbyid (r.id.msg); MyApp=(MYAPP) getapplication (); Textview.settext ("name---->"+myapp.getname ()); }}

Finally, note in the manifest file: First add in the label application

<activity android:name=". Otheractivity"></activity>

Then add the attribute Android:name= "in the application tag. MyApp "

Android uses global variables to pass data

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.