Android Study Notes (5)-transfer data through global variables

Source: Internet
Author: User

1. Global objects are a more practical way to transmit data between activities. For example, there are four scopes in Java Web. The Four Scopes are Page, Request, Session, and Application, the Application domain can be used and accessed anywhere in the Application, unless the Web server stops. The Global Object in Android is very similar to the Application field in Java Web. As long as the Android Application does not clear the memory, the global object can be accessed all the time ~

2. Create an Android project: "android_app" and enter "main. xml" to add a Button. The Code is as follows:

    
3. Create an "other. xml" in the current directory and add a TextView. The Code is as follows:

    
     
 
4. Create a class "OtherActivity" under the current package and inherit the Activity. Add an "onCreate" method. The Code is as follows:

package com.android.app;import android.app.Activity;import android.os.Bundle;public class OtherActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.other);}}

5. To use global variables in the program, you need to create a common class "MyApp" under the current package to inherit "Application ", then declare a "name" member and provide the constructor and add the "onCreate" Code as follows:

Package com. android. app; import android. app. application; public class MyApp extends Application {private String name; public String getName () {return name;} public void setName (String name) {this. name = name ;}@ Overridepublic void onCreate () {// TODO Auto-generated method stubsuper. onCreate (); setName ("James ");}}

6. Go to "Main. java", add Button and MyApp members, and then set a click event for the Button. The Code is as follows:

Package com. android. app; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. view; import android. widget. button; public class Main extends Activity {private Button button; private MyApp myApp; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) this. findViewById (R. id. button); // register a click event button for the button. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubmyApp = (MyApp) getApplication (); myApp. setName ("Jack"); // The modified name Intent intent = new Intent (Main. this, OtherActivity. class); startActivity (intent) ;}}@overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
7. Enter "OtherActivity. java", declare a MyApp member, and obtain the myApp. The Code is as follows:

package com.android.app;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class OtherActivity extends Activity {private MyApp myApp;private TextView textView;@Overrideprotected void onCreate(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("-appname-->>" + myApp.getName());}}
8. modify the configuration file "AndroidManifest. xml", add a new attribute "name" to the "Application" tag, and add an "Activity". The Code is as follows:

                        
                                 
              
         // The label added later    
9. Run the command. The result is as follows:



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.