Android Learning notes-passing data code samples via application _android

Source: Internet
Author: User
Tags stub
In the entire Android program, it is sometimes necessary to save some global data (such as user information) for easy invocation anywhere in the program. In the data transmission between the activity there is a way to compare the use of the global object, the use of Java EE should know the four scopes of Javaweb, where the application domain can be used and accessed anywhere in the application, unless the Web server is stopped, Global objects in Android are very similar to the application domains in Javaweb, and global objects will always be accessible unless the Android application clears the memory.

When application is started, the system creates a PID, the process ID, in which all activity runs on this main process. Therefore, all activity in the same application can be obtained through the Activity.getapplication () method to the same application object, inheriting application class, can access the custom data.

In simple terms, the following steps are used to pass data using application:
Create a new class, name MyApp, inherit the Android.app.Application parent class, and define the attributes that need to be saved in MyApp, such as user name, user type.
In the activity, the Activity.getapplication () method is used to get the MyApp object (which requires casting) to manipulate its data.
Modify the Android:name property (android:name=) of the application node in the Androidmanifest.xml file. MyApp ").

code Example
Step One:
Copy Code code 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 Two:
Copy Code code 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 Three:
Copy Code code as follows:

<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= "/>
<application
android:name=". M YApp "
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& Gt
</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.