Android Application Object

Source: Internet
Author: User

The Android Application and the Activity and Service are both components of the Android framework. This Application is usually automatically created when the app is started. The Application is a singleton mode in the app, and the Application is the longest object throughout the app lifecycle. All activities and services share an Application, so Application is usually used to share data, data transmission, and data cache.

I have consulted some materials on the Internet to supplement the Application:


1. when two activities need to transmit big data, try to avoid using Intent transmission. Instead, create a HashMap in the Application, and then the sender Activity places the data in the HashMap of the Application, then, the data index is transmitted to the receiver Activity through Intent. The receiver Activity can obtain the data transmitted by the sender from the Application.

2. when downloading data from the network, you can temporarily put it in the HashMap of the Application. When the app requests data again, if the Application already has data, you do not need to download it from the Internet again.

3. The app memory is limited. Therefore, if too much data is cached, you should write the cached data to the local rom or sdcard. ,

4. The Application is static, so some objects and data should not be referenced in the Application. The Application has a long life cycle. If the Application has an Activity control reference, the memory will not be released when the Activity wants to finish, resulting in Memory leakage.

 

When developing Java SE, If you want multiple classes to share a class, set the class to a static public object. In Android development, I personally think that to comply with Android development specifications, we should put the shared data into our own Application. Other classes can obtain shared data by obtaining the Application of the app. If you want to write your own Application class, you must inherit the Application class.


[Java]
Public class MyApplication extends Application {
// Data to be shared
Private int encrypted data;

@ Override
Public void onCreate (){
Required data = 100;
Super. onCreate ();
}
 
// Obtain shared data
Public int getaskdata (){
Return response data;
}
}

Public class MyApplication extends Application {
// Data to be shared
Private int encrypted data;

@ Override
Public void onCreate (){
Required data = 100;
Super. onCreate ();
}

// Obtain shared data
Public int getaskdata (){
Return response data;
}
}


When an Activity needs to access Shared data, it can obtain the Application of the app through the getApplicationContext () method, and forcibly convert the type to the MyApplicaiton object to obtain shared data.

[Java]
Public class MyActivity extends Activity {
Private MyApplication application;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
Application = (MyApplication) this. getApplicationContext ();
System. out. println ("Shared data" + application. getShareData ());
SetContentView (R. layout. main); // load the XML file
}
}

Public class MyActivity extends Activity {
Private MyApplication application;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
Application = (MyApplication) this. getApplicationContext ();
System. out. println ("Shared data" + application. getShareData ());
SetContentView (R. layout. main); // load the XML file
}
}

 

 

After that, you need to make some modifications in the AndroidManifest. xml file to inform our app that our Application uses MyApplication.


[Java]
<Application
Android: name = "com. shamoo. testapplication. MyApplication"
Android: label = "@ string/app_name"
Android: icon = "@ drawable/icon">
<Activity
Android: name = ". MyActivity">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
 
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>

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.