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