“This is the 12th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
Four major components
The four major components of Android include Activity, Service, Broadcase Receiver and Content Provider
Activity
An Activity is an interactive window between a user and an application. It displays controls and views, and can listen to user events and respond to them
1) Life cycle:
OnCreate () → onStart() → onResume() → Activity Running → onPause() → onStop() → onDestory()
OnCreate () : called when the Activity is created. Some initialization can be done here
OnStart () : called when the Activity changes from invisible to visible
OnResume () : The Activity is visible and can interact with the user
OnPause () : called when the system is about to start or resume another Activity
OnStop () : Activity changes from visible to completely invisible
OnDestory () : Activity is about to be destroyed
2) Problems encountered in the life cycle:
(1) The user jumps from A to B, assuming that B completely covers A:
A.pause () → B.create () → B.start () → B.Resume () → A.stop ()
(2) If the Back button is clicked:
B.pause () → A.start () → A.Resume () → B.Stop () → B.story ()
(3) If the Back button is pressed, the system returns to the desktop:
A.pause () → a.stop () → a.story ()
(4) If you press the Home button, the system returns to the desktop
A.o nPause () – > A.o nStop ()
3) The startup mode of the Activity
Task stack: The Android system uses the stack structure to manage the activities that are started during the running of applications, namely the Task stack
There are four startup modes for an Activity: Standard, singleTop, singleTask, and singleInstance
(1) Standard: The default startup mode. Each time an Activity is started, an instance is created, regardless of whether the instance has already been created
(2) singleTop: If the new Activity is at the top of the stack, it will not be recreated; If the new Activity already exists but is not at the top of the stack, the new Activity will still be created
(3) singleTask: As long as an Activity exists in a stack, multiple launches of the Activity will not recreate the instance. Instead, all activities above the Activity are removed from the stack until it is at the top of the stack.
(4) singleInstance: It is required that an Activity can only be created in a single task stack, and that only one instance of the Activity can exist in the task stack
(5) Common Flags for the Activity
FLAG_ACTIVITY_NEW_TASK: Specifies the “singleTask” startup mode for an Activity
FLAG_ACTIVITY_SINGLE_TOP: Specifies the “singleTop” startup mode for your Activity
FLAG_ACTIVITY_CLEAR_TOP: An Activity with this flag bit. When it starts, all activities above it in the same task stack are removed from the stack. If the Activity starts in standard mode, it will be removed from the stack itself, and the system will create a new instance of the Activity and place it at the top of the stack. As you can see from the previous section, the singleTask startup mode has the effect of this flag bit by default
Service
Android is the implementation of the program running in the background solution, it is very suitable to perform those tasks that do not need to interact with the user and also require long-term running. The service does not depend on any user interface, and it keeps running even if the application is switched to the background or the user opens another application
1) Life cycle
(1) startService:
OnCreate () → onStartCommand() → Service running → onDestory()
(2) bindService
OnCreate () → onBind() → onUnbind() → onDestory()
2) startService() differs from bindService() :
The started Service is started by another component calling the startService() method, which causes the service’s onStartCommand() method to be called. When a service is in the started state, its life cycle is independent of the component that started it and can run indefinitely in the background, even if the component that started the service has been destroyed. Therefore, the service needs to be stopped by calling the stopSelf() method or by calling the stopService() method by another component after completing the task
The bindService() method enables the service, binds the caller to the service, and terminates the service once the caller exits
Broadcase receiver
Broadcasting is a widely used mechanism for transferring information between applications. The broadcast receiver is a kind of component that filters and responds to the broadcast sent out
There are two ways to register broadcast receivers: application dynamic registration (using context.registerReceive () in run-time code) and static registration in the AndroidManifest file
The dynamic registration broadcast receiver features that the broadcast is disabled when the Activity used to register is turned off. Static registration does not need to worry about whether the broadcast receiver is turned off, as long as the device is on, the broadcast receiver is also on. This means that broadcasts subscribed to by the app will affect it when triggered, even if the app itself is not started
Content provider
Make a specified data set of one application available to other applications. Other applications can retrieve or store data from this content provider through the ContentResolver class
ContentProvider implements data sharing. ContentProvider is used to store and retrieve data and make it visible to all applications
The ContentProvider uses a URI to uniquely identify its data set, which is prefixed with Content :// to indicate that the data is managed by the ContentProvider
For example, address book data is consumed by multiple applications and must be stored in a content provider. It has the advantage of unifying data access