When a component of the Program is started, if no other component in the program is running, the Android system will start a process and a thread for the program. By default, all components in the same program run in the same process and thread (this thread is called the main thread ). If a program has a process when its component is started (this may be because another component of the program is running ), the component of the Program will be started in the existing process and run in the same thread (that is, the main thread. Of course, you can also run different components in the same program in different processes, and you can create separate threads for these processes.
This tutorial will mainly introduce how processes and threads in Android programs work.
Process
By default, all components of the same program run in the same process. For most programs, it is best not to change this point. However, if you find that you need to control a component to run in a specific process, you can set it through the android: process attribute in the manifest file.
In the manifest file, the <activity> <service> <receiver> <provider> labels of the four components support the android: process attribute. This android: the process attribute is used to determine the process in which the four components should run. You can set this attribute to make each component run in its own process, or make some components run in the same process and others run in another process. You can even set android: process to run components of different programs in the same process, of course, the premise is that these different programs should have the same Linux User ID and sign the same certificate (sign with the same cerificates ).
In the <application> label of manifest, you can also set the android: process attribute so that all components in the program will run in the same process.
When there are few memory resources on the Android device and more memory resources are required for processes currently interacting with users, the Android system may kill some processes. Program components running in the killed process are also destroyed. When you use these components again, the system starts the new processes.
When the system resources are insufficient and the system decides to kill some processes, it is determined based on the Relevance between these processes and users. For example, compared with processes where activities are visible, the system is more likely to kill processes where invisible activities are located. Therefore, when the system decides which process to kill, it mainly determines based on the status of components running in the process. This will be further discussed in the next article.