Home > Article > Web Front-end > Android interview questions: four major components
Recommended: "2020 Android interview questions summary [Collection]"
2. Do not store data in the Application class and global singleton class, as this will cause the app to be unable to restore its state correctly. Temporary data during runtime should be stored in SharedPreference, temporary files or databases
3 The intent mechanism provided by the system should be used to transfer data between activities.
//Specify singleTask mode, which has the same effect as specifying android:launchMode "singleTask" in AndroidManifest.xml
Intent.addFlags(Intent .FLAG_ACTIVITY_SINGLE_TOP);
//Specify singleTop mode, which has the same effect as specifying android:launchMode "singleTop" in AndroidManifest.xml
Intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//Has this When the flagged Activity is started, it is in the same task stack, so the Activity above it will be popped out of the stack, and will generally appear together with the singleTask mode
Intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
// Activities with this flag will not appear in the historical Activity list. It is equivalent to specifying android: excludeFromRecents="true" in AndroidManifest.xml
The priority of the flag bit is higher than the priority specified in AndroidManifest
The following figure describes the relationship between Fragment and Activity life cycle very well
1. Let’s start with the basics ---> Life cycle
Activity has 7 life cycles: onCreate(); onStart(); onResume(); onPause(); onStop(); onDestroy(); onRestart();
Fragment has 11 life cycles: onAttach(); onCreate(); onCreateView(); onActivityCreate(); onStart(); onResume(); onPause (); onStop(); onDestroyView(); onDestroy(); onDetach();
So Fragment will be more flexible than Activity, because there are more life cycles, and there are more places you can control. .
2. In terms of flexibility
Activity is one of the four major components. It is the carrier of each page. One is one. The display of Fragment depends on Activity. From the life of Fragment You can find out during the cycle.
Fragment is a small fragment one by one
1) Compared with Activity, it is more flexible. It can be written directly in the XML file or dynamically added in the Activity;
2) You can use show()/hide() or replace() to switch Fragments at any time, and there will be no obvious effect when switching, and the user experience will be better; although Activity can also be switched, but Activity Switching between will have obvious page turning or other effects. Switching a small part of the content will not give the user a good feeling
The display of Fragment depends on Activity, which can be understood from the life cycle of Fragment.
Similar to the menu bar below WeChat, as well as mobile phone and tablet adaptation, etc.
https://www.cnblogs.com/huihuizhang/p/7623760.html
start is started directly, and bound is bound to the current activity.
If a Service is started and bound again, the Service will always run in the background. And no matter how it is called, onCreate will always be called only once, corresponding to how many times startService is called, Service's onStart will be called as many times. Calling unbindService will not stop the Service, but must call stopService or Service's stopSelf to stop the service.
https://blog.csdn.net/geyunfei_/article/details/78851024
Service runs in the main thread. Generally, time-consuming operations cannot be performed in Service. If necessary, you can use remote Service to start a new process.
AlarmManager provides access to system alarm services. These allow you to run the application at some point in the future. When an alert sounds, the system broadcasts the registered intent and automatically launches the target application if it is not already running. Registered alarms are retained while the device is sleeping (with the option to wake it up if the device malfunctions during this period), but are cleared if the device is turned off and restarted. The alarm manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing. This ensures that the phone won't go to sleep until you've processed the broadcast. Once onReceive() returns, the alarm manager will release this wake lock. This means that, in some cases, the phone will sleep as soon as the onReceive() method completes. If your alert receiver calls Context.startService(), the phone may sleep before starting the requested service. To prevent this from happening, your BroadcastReceiver and Service will need to implement a separate wake lock policy to ensure that the phone continues to run until the service is available.
Foreground service is a service that is visible to users. Foreground services can be created in the form of notifications
ActivityManagerService (hereinafter referred to as AMS) is the core service in Android. It is mainly responsible for the startup, switching, scheduling of the four major components in the system and the management and scheduling of application processes. Its responsibilities are the same as those of the processes in the operating system. The management and scheduling modules are similar, so it is very important in Android
onStartCommand
In the method, return <strong>START_STICKY</strong>
StartCommand()Several constants:
The system re-creates the service and calls the method, but does not pass the last passed
intent, but only passes an empty
intent. Unless there is an
intent to be delivered, then these
intent will be delivered. This is suitable for services such as players, which do not need to execute commands. They only need to run alone and wait for tasks.
The system does not recreate the service unless there is an to be delivered. This is the safest option and prevents services from running when unnecessary.
The system re-creates the service and calls the method, passing the last passed
intent. The remaining
intent that needs to be passed will be passed in order. This is suitable for services like downloads, which resume immediately and perform aggressively.
Improve Service Priority
Foreground services are considered to be used for known running services and will not be killed first when the system needs to release memory. Drop the process.Send a broadcast in onDestory() to open your own
service broadcast method, that is, when the service callsondestory(), send a Customized broadcast, when receiving the broadcast, restart the service. Of course, this solution is feasible in theory, and it is also feasible to experiment with the results. But in some cases, the broadcast sent is ranked lower in the message queue, and the service may be destroyed before receiving the broadcast (just a guess). So in order to make this mechanism work perfectly, you can start two services, monitor each other, and start each other. Service A listens to B's broadcast to start B, and service B listens to A's broadcast to start A. After experiments, this solution is feasible.
The above is the detailed content of Android interview questions: four major components. For more information, please follow other related articles on the PHP Chinese website!