[1] Activity
1. Lifecycle
onCreate: Indicates that the activity is being created (some initialization can be done in this method)
onRestart: Indicates that the activity is being restarted and will be called when the current activity changes from invisible to visible
onStart: Indicates that the activity is being started. At this time, the activity is already visible, but it does not appear in the foreground and cannot interact with the user.
onResume: Indicates that the activity is already visible, in the foreground, and can interact with the user
onPause: indicates that the activity is stopping
onstop:
ondestory:
2. Life cycle analysis under abnormal conditions
When the activity is terminated in an abnormal situation, the system will call onSaveInstanceState to maintain the state of the current activity;
After the activity is recreated, the system calls onRestoreInstanceState, and passes the Bundle object saved when the activity is destroyed as a parameter to the onCreate and onRestoreInstanceState methods at the same time
3. The loading mode of the activity
standard: default mode
singleTop: stack top mode (activity is reused at the top of the stack, and rebuilt when it is not) Applicable scenarios: display page for push messages
singleTask: In-stack reuse mode There will be no repeated instances in the stack, and all activities on the head will be killed when reused. Adapted to the scene: the entry of the program, used in the startup page and the home page
singleInstance: The single instance mode re-opens a new task stack to adapt to the scene: a page separated from the program (similar to an alarm clock prompt)
【2】Broadcast
Broadcasts can come from the system or from other apps;
Broadcasting is divided into ordered broadcasting and unordered broadcasting;
Intent is used to send broadcasts, BroadcastReceiver is used to receive broadcasts
Dynamic registration must wait until the app starts to receive broadcasts, and static registration can realize that applications that are not started can also receive broadcasts;
【three】service
1. Attributes of the manifest file
exported: represents whether it can be implicitly called by other applications
process: Does it need to be run in a separate process
enable: whether it can be instantiated by the system
2. How to ensure that the service is not killed
Killing a service due to insufficient memory: You can set the return value of the onStartCommand method to Start_sticky, which means that after the service is killed when the memory resources are tight, it will resume when the memory resources are sufficient; you can also set the service as a foreground service, which has more High comparative level, so that it won't be killed when memory resources are tight
The user manually intervenes to kill the service: the non-forced shutdown of the service will go through the life cycle, the onDestory method will be called, and the broadcast can be sent in this method and restarted (for the program to be more sound, we can open two services and listen to each other, Start each other, service A listens to B's broadcast to start B, service B listens to A's broadcast to start A)