Android transmits data through Application, androidapplication
</pre><pre>
Package com. example. applicationTest; import android. app. application;/*** Created by chang on 14-10-1. */public class App extends Application {public String getName () {return name;} public void setName (String name) {this. name = name;} public String name; @ Override public void onCreate () {super. onCreate (); setName ("James ");}}
package com.example.ApplicationTest;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MyActivity extends Activity { private Button btnLaunch = null; private App myApp; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnLaunch = (Button)this.findViewById(R.id.btnLaunch); myApp = (App)this.getApplication(); myApp.setName("hello"); btnLaunch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MyActivity.this,OtherActivity.class); startActivity(intent); } }); }}
package com.example.ApplicationTest;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;/** * Created by chang on 14-10-1. */public class OtherActivity extends Activity { private App myApp; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other); textView = (TextView)findViewById(R.id.showMsg); myApp = (App)getApplication(); textView.setText(myApp.getName()); }}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.ApplicationTest" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="19"/> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:name=".App"> <activity android:name="MyActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=".OtherActivity" > </activity> </application></manifest>
How does Android transmit data between Activity and Service?
1. Raw Data Type: Temporary raw data is transmitted between Activity/Servier, and data can be transmitted using the Intent putExtras method. If the transmitted data needs to be saved for a long time, the SharedPreference class is used. 2. Transfer objects. When you pass objects that do not need to be saved for a long time between Activity and Servier, you can use the following methods: (1) through the Application class, each Android Application has an Application class. When you set a name for the Application in AndroidManifest. xml of the program, your program must have a subclass of Application. This Application subclass is automatically instantiated by Android and is a family-class. its lifecycle is the same as that of the program, you can save some global objects in the Application class. The Application class can be obtained through getApplication. (2) Pass the object through HashMap of WeakReferences. When an Activity needs to pass an object to another Activity, you can use a keyword to store the object in a HashMap, and send this keyword to the target Activity through Internt Extras, after receiving this keyword, the target Activity uses this keyword to extract the object from HashMap.
How to transmit custom data between android activities
You can use global variables.
1. Customize the derived class MyApplication of an Application, and specify MyApplication as your application name in Manifest.
2. Define the variables in MyApplication that you want to pass the data type. Add a member function to obtain the reference of the variable.
3. Of course, synchronization is required for reading and writing the corresponding variables in MyApplication.
---------------------------------------------------------------------
For more questions and answers, please follow @ @ Sina Weibo