Android learning notes-sample code for passing data through Application

Source: Internet
Author: User

In the entire Android program, you sometimes need to save some global data (such as user information) for convenient calling anywhere in the program. There is a relatively useful method for data transmission between activities, that is, global objects. If you have used J2EE, you should know the Four Scopes of JavaWeb, 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 domain in Java Web, unless the Android Application clears the memory, otherwise, global objects are always accessible.

When the Application is started, the system creates a PID, that is, the process ID, and all the activities will run on this main process. Therefore, all the activities in the same Application can obtain the same Application object through the Activity. getApplication () method, inherit the Application class, and then access the custom data.

To put it simply, follow these steps:
Create a new class named MyApp, inherit the parent class of android. app. Application, and define the attributes to be saved in MyApp, such as the user name and user type.
In the Activity, you can use the Activity. getApplication () method to obtain the MyApp object (forced conversion is required) and operate its data.
Modify the android: name attribute of the application node in the AndroidManifest. xml file (android: name = ". MyApp ").

Sample Code
Step 1:Copy codeThe Code is as follows: 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:Copy codeThe Code is as follows: 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:Copy codeThe Code is as follows: <manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "cn. bgxt. staticchuandi"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<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 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.