Android study notes data code samples are passed through Application

  • 2020-05-10 18:50:51
  • OfStack

Throughout the Android program, there are times when some global data (such as user information) needs to be saved for easy invocation anywhere in the program. Data transfer between Activity is used in one way, is a global object, used J2EE should know JavaWeb four scope, including Application domain anywhere in the application and access can be used, unless it is Web server is stopped, the global object is very similar to the Application JavaWeb Android domain, unless it is Android application clear memory, or global object would be 1 straight can access.

When Application is started, one PID, or process ID, is created, and all Activity will run on this main process. Therefore, all Activity in the same 1Application can obtain the same Application object through the Activity.getApplication () method, inherit the Application class, and access the custom data.

In simple terms, the steps for transferring data using Application are as follows:
Create a new class, name it MyApp, inherit the android.app.Application parent class, and define the properties to be saved in MyApp, such as: user name, user type.
In Activity, the Activity.getApplication () method is used to obtain the MyApp object (which needs to be cast) and manipulate its data.
Modify the android:name attribute (android:name=".MyApp ") of the application node in the AndroidManifest.xml file.

Code sample
Step 1:
 
public class MyApp extends Application { 
private String name; 
public String getName() { 
return name; 
} 
public void setName(String name) { 
this.name = name; 
} 
@Override 
public void onCreate() { 
// TODO Auto-generated method stub 
super.onCreate(); 
setName("Dick"); 
} 
} 

Step 2:
 
public class MainActivity extends Activity { 
private Button btn; 
private MyApp myApp; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
btn=(Button)this.findViewById(R.id.btn); 
btn.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
// TODO Auto-generated method stub 
myApp=(MyApp)getApplication(); 
myApp.setName("jack"); 
Intent intent=new Intent(MainActivity.this, otherActivity.class); 
startActivity(intent); 
} 
}); 
} 
} 

Step 3:
 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="cn.bgxt.staticchuandi" 
android:versionCode="1" 
android:versionName="1.0" > 
<uses-sdk 
android:minSdkVersion="8" 
android:targetSdkVersion="17" /> 
<application 
android:name=".MyApp" 
android:allowBackup="true" 
android:icon="@drawable/ic_launcher" 
android:label="@string/app_name" 
android:theme="@style/AppTheme" > 
<activity 
android:name="cn.bgxt.staticchuandi.MainActivity" 
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"/> 
</application> 
</manifest> 

Related articles: