Use global variables in Android. In addition to public static variables, you can also use Android.app.application. The Android system creates one and only one Application class object for each Application run time, Therefore, Application can be said to be a singleton class, so the object obtained in different activities and services is the same object. Therefore, some operations such as data transfer, data sharing and data caching are carried out through Application. When an Application is launched, the system creates a PID, which is the process ID, on which all activities run. Global variables are initialized when the Application is created. All activities in the same Application get the values of these global variables. Can be used to save login status. The value of a global variable in an Application is changed in the Activity, and its lifetime is equal to the lifetime of the entire Application

directory

– Life cycle

– The Activity listens for its life cycle

The life cycle

Introduce the Application lifecycle and what you can do in these lifecycle methods

OnCreate is executed when the program is created

Do not perform time-consuming operations on third-party library initialization (network request, push, map, sharing, etc.), MultiDex (subcontracting configuration), global objects, environment configuration variables, and global data shared storage, otherwise the startup speed of App will be affected

Execute when onTerminate terminates

OnConfigurationChanged Triggers this method when the configuration changes.

Screen rotation

OnLowMemory is executed when the memory is low

Clear the photo resource (GlideApp use) buffer

The onTrimMemory program executes during memory cleanup

Photo resources (GlideApp use), background services, according to different levels to decide whether to clear the buffer

Supplement: registerComponentCallbacks, ComponentCallbacks2 callback interface, it will rewrite onTrimMemory, onLowMemory, onConfigurationChanged. ComponentCallbacks back to the interface with one less onTrimMemory function

The Activity’s lifecycle listener

RegisterActivityLifecycleCallbacks and unregisterActivityLifecycleCallbacks function, RegisterActivityLifecycleCallbacks all activities within the application life cycle to monitor, when change the Activity within the application life cycle is called, ActivityLifecycleCallbacks interface there is a corresponding method returns the corresponding Activity life cycle state. UnregisterActivityLifecycleCallbacks logout