Data is transmitted between activities through global variables. activity global variables

Source: Internet
Author: User

Data is transmitted between activities through global variables. activity global variables
Transfer Data between activities using global variables

I. Introduction

The onCreate method in the Application field is the entrance of the Android program. When the Android program runs, it automatically loads the Application object. It feels that the Main method should be encapsulated in it.

The intent method and global variable method are used to transmit data in the activity.

 

Ii. Procedure

1. Create a New MyApplication classInherit Application class

Public class MyApplication extends Application

After inheritance, the MyApplication class is global.

2. Call the MyApplication class on the data transmission page and generate data

MyApplication app = (MyApplication) getApplication ();

App. setName ("fry ");
App. setAge (22 );

3. Call the MyApplication class on the receive data page and output data.

MyApplication app = (MyApplication) getApplication ();

TV _answer.setText ("app:" + app );

4. In AndroidManifest. xmlConfiguration FileAdd the Android STARTUP configuration of the MyApplication class in

Android: name = "activityGlobelVariable. MyApplication"

5. Success

 

Iii. Specific code examples

The code is optimized twice:

First, the MyApplication class is implementedSingleton ModeTo facilitate calling in a class that does not inherit the Activity

Call:

MyApplication app = MyApplication. getInstance ();

Second, for transmitting multiple groups of dataHashMap

Private HashMap <String, Object> map = new HashMap <String, Object> ();

 

Result chart:

ActivityGlobelVariable. MyApplication

1 package activityGlobelVariable; 2 3 import java. util. hashMap; 4 5 import android. app. application; 6 7 public class MyApplication extends Application {8 9 10 // you can also use hashMap to replace these private variables 11Private HashMap <String, Object> map = new HashMap <String, Object> ();12 13 14 private String name; 15 private int age; 16 17 18 19 20 21 public HashMap <String, Object> getMap () {22 return map; 23} 24 25 public void setMap (HashMap <String, Object> map) {26 this. map = map; 27} 28 29 30 31 32 33 34 35 36 37 3839 private static MyApplication instance = null; 40 41 public static MyApplication getInstance () {42 return instance; 43}44 45 public String getName () {46 return name; 47} 48 49 50 51 public void setName (String name) {52 this. name = name; 53} 54 55 56 57 58 public int getAge () {59 return age; 60} 61 62 63 64 public void setAge (int age) {65 this. age = age; 66} 67 68 69 70 71 public MyApplication () {72 super (); 73} 74 75 76 77 public MyApplication (String name, int age) {78 super (); 79 this. name = name; 80 this. age = age; 81} 82 83 84 85 @ Override86 public void onCreate () {87 // TODO Auto-generated method stub 88 super. onCreate (); 89 this. instance = this; 90}91 92 93 94 95 96 97 99 100 101 102 @ Override103 public String toString () {104 return "MyApplication [name =" + name + ", age = "+ age +"] "; 105} 106 107 108 109}

ActivityGlobelVariable. MainActivity

1 package activityGlobelVariable; 2 3 4 5 6 import com. example. activityGlobelVariable. r; 7 8 import android. app. activity; 9 import android. content. intent; 10 import android. OS. bundle; 11 import android. view. view; 12 import android. view. view. onClickListener; 13 import android. widget. button; 14 15 16 17 public class MainActivity extends Activity {18 private Button btn_openActivty; // create a button Object 19MyApplication app = MyApplication. getInstance ();20 protected void onCreate (Bundle savedInstanceState) {21 super. onCreate (savedInstanceState); // action of the parent class 22 setContentView (R. layout. activity_main); // introduce the interface named activity_main 23 btn_openActivty = (Button) findViewById (R. id. btn_openActivity); // find the button24 btn_openActivty.setOnClickListener (new OnClickListener () for btn_openActivity. {// set the button to listen to 25 26 @ Override27 public void onClick (View v) {// onclick event 28 // TODO Auto-generated method stub29 Intent intent = new Intent (MainActivity. this, Activity01.class); // initialize intent30 // Application domain access 31 //MyApplication app = (MyApplication) getApplication ();32App. setName ("fry ");33App. setAge (22 );34 35 // HashMap application 36App. getMap (). put ("data1", "data1 ");37App. getMap (). put ("data2", 2.2 );38 startActivity (intent); // enable activity39} 40}); 41} 42 @ Override43 protected void onDestroy () {44 // TODO Auto-generated method stub45 super. onDestroy (); 46 app. getMap (). remove ("data1"); 47 app. getMap (). remove ("data2"); 48} 49}

ActivityGlobelVariable. Activity01

Package activityGlobelVariable; import com. example. activityGlobelVariable. r; import android. app. activity; import android. OS. bundle; import android. widget. textView; public class Activity01 extends Activity {private static final MyApplication = null; private TextView TV _answer; @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. activity01); TV _answer = (TextView) findViewById (R. id. TV _answer);/** getApplication () is the method in the Activity class. Other classes do not * Because MyApplication inherits the Application, is global in the configuration file to configure MyApplication * MyApplication app = (MyApplication) getApplication (); * The above sentence is to access and call MyApplication *///MyApplication app = (MyApplication) getApplication ();        MyApplication app = MyApplication. getInstance ();// HashMap application String s1 = (String) app. getMap (). get ("data1"); double d1 = (Double) app. getMap (). get ("data2"); TV _answer.setText ("app:" + app + "\ ns1:" + s1 + "\ nd1:" + d1 );}}

/ActivityGlobelVariable/AndroidManifest. xml

 1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2     package="com.example.activityGlobelVariable" 3     android:versionCode="1" 4     android:versionName="1.0" > 5  6     <uses-sdk 7         android:minSdkVersion="8" 8         android:targetSdkVersion="19" /> 9 10     <application11         android:name="activityGlobelVariable.MyApplication"12         android:allowBackup="true"13         android:icon="@drawable/ic_launcher"14         android:label="@string/app_name"15         android:theme="@style/AppTheme" >16         <activity17             android:name="activityGlobelVariable.MainActivity"18             android:label="@string/app_name" >19             <intent-filter>20                 <action android:name="android.intent.action.MAIN" />21 22                 <category android:name="android.intent.category.LAUNCHER" />23             </intent-filter>24         </activity>25         <activity android:name="activityGlobelVariable.Activity01" android:exported="true"></activity>26     </application>27 28 </manifest>

 

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.