[[435976]] Preface The Application class is something we will encounter in every development. Every APP will have an Application instance, which has the same life cycle as the APP. The Application and the APP "live and die together"; Let's talk about it today, strengthen your foundation and inner strength, and make yourself more confident; 1. What is Application?- The Android system creates one and only one Application class object for each program running, so Application can be said to be a class in singleton mode;
- The life cycle of the application object is the longest in the entire program, and its life cycle is equal to the life cycle of the program. Since it is a global singleton, the objects obtained in different Activities and Services are the same object;
- Application, like Activity and Service, is a system component of the Android framework. When an Android program is started, the system will create an Application object to store some system information;
- Usually we don't need to specify an Application, the system will automatically create one for us. If you need to create your own Application, it is also very simple. Create a class that inherits Application and register it in the application tag in the AndroidManifest.xml file;
- When you start an application, the system creates a PID, or process ID, and all activities run on this process.
- The life cycle of the Application object is the longest in the entire program. Its life cycle is equal to the life cycle of the program. Because it is a global singleton, the objects obtained in different Activities and Services are the same object. Therefore, some operations such as data transfer, data sharing, and data caching can be performed through the Application.
2. Methods of the Application class 1. onCreate() method Inherited from ContextWarpper class - public class Application extends ContextWrapper implements ComponentCallbacks2 {
- ...
- ...
- }
- public void onCreate() {
- }
- It is called when the Application is created, and is generally used to initialize some things, such as global objects and environment configuration;
- Set up global shared data, such as global shared variables, methods, etc.;
Notice: Shared data is only valid during the life cycle of the application. When the application is killed, the data will also be cleared, so only some temporary shared data can be stored. Please do not perform time-consuming operations, otherwise it will slow down the application startup; 2. onConfigurationChanged(Configuration newConfig) Rewrite this method to monitor some configuration information change events of APP (such as screen rotation, etc.); This method is called when the configuration information changes; - registerComponentCallbacks(new ComponentCallbacks2() {
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- ...
- }
- });
3. onLowMemory() method - Rewrite this method to monitor events when the overall memory of the Android system is low;
- Calling time: when the overall memory of the Android system is low;
- registerComponentCallbacks(new ComponentCallbacks2() {
- @Override
- public void onLowMemory() {
- }
- });
4. onTerminate() method - Notify the application of current memory usage (identified by memory level)
- Release memory resources to different degrees according to current memory usage to avoid being killed directly by the system & optimize the performance experience of the application;
- When the system is low on memory, it will kill processes from the lowest to the highest in the LRU Cache, giving priority to killing applications that occupy the most memory.
- registerComponentCallbacks(new ComponentCallbacks2() {
- @Override
- public void onTrimMemory( int level ) {
- // The Android system will pass in the corresponding level based on the current memory usage
- // The following example uses clearing the cache as an example
- super.onTrimMemory( level );
- . if ( level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
- mPendingRequests.clear();
- mBitmapHolderCache.evictAll();
- mBitmapCache.evictAll();
- }
- });
5. registerActivityLifecycleCallbacks() and unregisterActivityLifecycleCallbacks() These two methods are used to register or unregister the life cycle monitoring of all Activities in the APP; When the life cycle of the Activity in the APP changes, the method in ActivityLifecycleCallbacks will be called: - registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
- @Override
- public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
- Log.e(TAG, "onActivityCreated: " + activity.getLocalClassName());
- }
- @Override
- public void onActivityStarted(Activity activity) {
- Log.e(TAG, "onActivityStarted: " + activity.getLocalClassName());
- }
- @Override
- public void onActivityResumed(Activity activity) {
- Log.e(TAG, "onActivityResumed: " + activity.getLocalClassName());
- }
- @Override
- public void onActivityPaused(Activity activity) {
- Log.e(TAG, "onActivityPaused: " + activity.getLocalClassName());
- }
- @Override
- public void onActivityStopped(Activity activity) {
- Log.e(TAG, "onActivityStopped: " + activity.getLocalClassName());
- }
- @Override
- public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
- }
- @Override
- public void onActivityDestroyed(Activity activity) {
- Log.e(TAG, "onActivityDestroyed: " + activity.getLocalClassName());
- }
- });
Check the log printout as follows: - onActivityPaused: MainActivity
-
- onActivityStopped: MainActivity
-
- onActivityStarted: MainActivity
-
- onActivityResumed: MainActivity
6. registerComponentCallbacks() and unregisterComponentCallbacks() methods Register and unregister ComponentCallbacks2 callback interface - registerComponentCallbacks(new ComponentCallbacks2() {
- @Override
- public void onTrimMemory( int level ) {
- }
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- }
- @Override
- public void onLowMemory() {
- }
- });
3. Application Scenarios and Customization 1. Application scenarios- Initialize application-level resources, such as global objects;
- Data sharing and data caching;
- Get the current memory usage of the application and release resources in time to avoid being killed by the system;
- Monitor changes in application configuration information, such as screen rotation;
- Monitor the life cycle of all Activities in the application;
2. Customize Application (1) Inherit Application - public class MyApplication extends Application{
- /**Declare variables*/
- private String value;
- @Override
- public void onCreate() {
- super.onCreate();
- // Initialize global variables
- setValue(VALUE);
- }
- public void setValue(String value){
- this.value = value;
- }
- public String getValue(){
- return value;
- }
- }
Note: Inherit the Application class and mainly rewrite the onCreate() method (the onCreate() method of the android.app.Application package is the real entry point of the Android program), which is to initialize the value of the variable when creating it. Then you can operate on the variable in each file in the entire application; (2) Configure a custom Application in the ApplicationManifest.xml file - <application
- android: name = "MyApplication" >
- </application>
(3) Using a custom Application class instance - private MyApplication app;
- // Just call Activity.getApplication() or Context.getApplicationContext() to get an Application object
- app = (MyApplication) getApplication();
- //Then get the corresponding member variables or methods
- app.getValue();
Summarize When each Android App is running, it will first automatically create an Application class and instantiate an Application object, and there is only one Application class, which is a singleton class; That is, different components (such as Activity, Service) can obtain the Application object and they are all the same object; The life cycle of the Application object is the longest in the entire program, which is equal to the life cycle of the Android App. |