Application Lifecycle (a)

1. What Application that?

Application and Activity, as Service, is a system component android framework, the system will create an android application object when the program starts

For some of the information storage system. Usually we do not need to specify an Application, then the system will automatically help us create

If you need to create your own Application is also very simple to create a class that inherits Application and registration application label manifest in

(Application label just give a name attribute to increase their Application to the name given).

 

Android operating system created for each program an object class, and create an Application only one class can be understood as a single-mode embodiment.

Its life cycle is the longest of the entire program is running because it is global singleton, so get in different Activity and Service in an object.

Application to typically use some data transfer, data cache operations.

2, Application Lifecycle

Application contains five methods disclosed
. 1) void onConfigurationChanged (the Configuration newConfig)
2) void the onCreate () 
. 3) void onLowMemory ()
. 4) void OnTerminate ()
. 5) void onTrimMemory ()

 

The first is triggered when the configuration is changed
and the second is created when the program creates
a third trigger memory is not enough
to call to terminate the program when the fourth but can not guarantee to call
the fifth is triggered when memory scrubbing

 

3, the example shows

 1 /**
 2  * @author ljcheng
 3  * @date 2019/6/29
 4  */
 5 public class MyApplication extends Application {
 6 
 7     private static String TAG = "MyApplication";
 8 
 9     @Override
10     public void onCreate() {
11         // 程序创建的时候执行
12         KLog.d(TAG, "onCreate");
13         super.onCreate();
14     }
15 
16     @Override
17      public  void OnTerminate () {
 18          // when the program terminates execution
 19          // Called when the termination of the application object, is not guaranteed to be called when the program is terminated by the kernel in order to free up resources for other applications, then
 20          // it will not be reminded, and onTerminate method does not call the application object directly terminate the process 
21          KLog.d (TAG, "onTerminate" );
 22          Super .onTerminate ();
 23      }
 24-  
25      @Override
 26      public  void onLowMemory ( ) {
 27          // when the daemon has terminated the lack of resources also calls this method. Good applications typically do not have to release some of this method in which
 28          // resources to be used to deal with the situation when the daemon has terminated, the foreground application when the memory is not enough. 
29          KLog.d (TAG, "onLowMemory");
 30          Super .onLowMemory ();
 31 is      }
 32  
33 is      @Override
 34 is      public  void onTrimMemory ( int Level) {
 35          // program is executed when the memory cleanup 
36          KLog.d (the TAG, "onTrimMemory" );
 37 [          Super .onTrimMemory (Level);
 38 is      }
 39  
40      @Override
 41 is      // trigger the method of configuration change 
42 is      public  void onConfigurationChanged (the configuration newConfig) {
 43 is          KLog.d (the TAG, "onConfigurationChanged" );
 44 is         super.onConfigurationChanged(newConfig);
45     }
46 
47 }

 

Fourth, to sum up

1.Application during the entire life cycle of the application is the longest running

2. Specific Applicatiom killed on the situation in the above code has been reflected in the following quote some cases other people's summary

 

Note: application cases killed Analysis:
In order to determine which process to kill time in low memory, Android will run according to the components and their status in the course of these processes into a "level of importance" important. the sorted according to the following rules:

1: a front-end process can be run in the process of holding the forefront of the screen and interact with the user of the Activity (onResume method is called), can also be held IntentReceiver a running (that is to say he is performing his onReceiveIntent method) process. in the system, only a small number of such processes, and unless memory has been low enough to run these processes, otherwise the system will not take the initiative to kill these processes. in this case, the device typically has reached the required memory consolidation state, so kill these processes in order to keep the user interface to stop responding.

2: visualization process is held by a user to be visible, but the process Activity in (when onPause method is called) does not show the forefront of example, this process usually occurs in a front-end Activity appears in a dialog. when a front retainer Activity visible. this process system is considered to be extremely important, and usually will not be killed unless all front-end processes in order to maintain the normal operation have to kill these processes visible.

3: The service process is to hold a Service of process, the Service is initiated by startService () method, although the user can not directly see these processes, but usually they do work is of great concern to the user (for example, played in the background mp3 download or upload files in the background), so unless all front-end processes in order to maintain and visualization processes running, the system is not going to kill the service process.

4: Processes are holding process Activity (when onStop () method is called) of a user is no longer visible to those processes do not directly affect the user experience to join these processes have complete, correct completion of his own life. period (visit Activity for more details), it will be killed at any time in the process of freeing memory for the first three of these background processes usually have a lot of background processes running, so these processes are stored in a LRU list to ensure low memory when the user has recently seen a process will be finally killed

5: empty process is the process does not hold any active application components only reason to retain this process is to provide a caching mechanism to shorten the startup time of his next run the application on its own, to kill the system. the purpose of these swap process is to balance overall system resources between these empty cached processes and the underlying core

 

In the new study, if not the right place also hope you understand, welcome comments that problem.

Guess you like

Origin www.cnblogs.com/livexiaojie/p/11106691.html