Home  >  Article  >  Web Front-end  >  Android interview questions: four major components

Android interview questions: four major components

藏色散人
藏色散人forward
2020-07-31 14:27:084577browse

Recommended: "2020 Android interview questions summary [Collection]"

Android interview questions (four major components)

Window, process, thread article

Android interview questions (data storage, view article)

Activity

## Q: Tell me about the life cycle of Activity?

Q: What is the difference between onStart() and onResume()/onPause() and onStop()?

The difference between whether it is in the foreground and visible to the user

Q: What methods will be called back when Activity A starts another Activity B? What if Activity B is completely transparent? What if a Dialog is started?

A will call back onPause()>>onStop(), transparent will not call onStop(), and the dialog box will not call onPause() and onStop()

Q: Talk about the onSaveInstanceState() method? When will it be called?

This method will be called when the Activity is accidentally destroyed and then re-created. For example, switching between horizontal and vertical screens will cause the Activity to be re-created. The onSaveInstanceState() method is called before onStop() to save the state of the current Activity. , when the Activity is re-created, onRestoreInstanceState() will be called to restore the state of the Activity. onRestoreInstanceState() is called before onStart().

Q: What is the difference between onSaveInstanceState() and onPause()?

There is no fixed order in which the two are executed.

The triggering timing of the onsavedinstance (Bundle savedinstancestate) method. The typical scenario is pressing the home button or switching activities. Such activities may be destroyed. However, if you press the back button to exit the program, this method will not be called. , suitable for saving some non-persistent data (that is, data that needs to be stored while the program is running).

Onpause() must be called whether it is destroying or exiting the program. It is suitable for saving persistent data. However, Android itself does not provide a bundle parameter for this method, so we can choose to make a static variable or It provides a sharedpreference as a data carrier.

Q: How to avoid Activity reconstruction when configuration changes?

When registering each activity in the manifest file, write

android:configChanges="XXX"

For example, switching between horizontal and vertical screens: android:configChanges="orientation"

Q: How can a low-priority Activity be restored to its pre-destruction state after being recycled due to insufficient memory?

1. When the app is in the background and is recycled by the system, the app's process is killed, the Activity is also recycled, and the app's task and activity stacks and corresponding intents and data will be saved by the system. When the app is switched back to the foreground, the system restores the task and activity stacks and corresponding intents and data.

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.

Q: Tell us about the four startup modes of Activity? (Sometimes a practical question will arise to analyze the situation of Activity in the return stack)

https://blog.csdn.net/mountain_hua/article/details/81481242

Q: Let’s talk about the difference between singleTop and singleTask and application scenarios

Stack top reuse: Solve the problem of repeatedly opening Activity.

In-stack reuse: When a task stack wants to call the Activity of another program, as follows:

Q: onNewIntent() Call time?

When performing singleTop and singleTask modes, there is a corresponding Activity, and the onNewIntent() of the Activity will be called.

Q: What are the flag bits of Activity startup mode?

Intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

//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

Q: How to start the Activity of other applications?

SingleTask and taskAfiinity are used together

Q: Activity startup process?

https://www.jianshu.com/p/9ecea420eb52

Fragment


Q: Talk about the life cycle of Fragment?

The following figure describes the relationship between Fragment and Activity life cycle very well

Q: What are the similarities and differences between Activity and Fragment?

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

Q: What is the relationship between Activity and Fragment?

The display of Fragment depends on Activity, which can be understood from the life cycle of Fragment.


Q: When would you consider using Fragment?

Similar to the menu bar below WeChat, as well as mobile phone and tablet adaptation, etc.

Service


Q: Let’s talk about the life cycle of Service ?

https://www.cnblogs.com/huihuizhang/p/7623760.html


Q: What are the two startup methods for Service? What's the difference?

start is started directly, and bound is bound to the current activity.


Q: After an Activty starts a Service first, what method will be called back when it is bound? What can be done to call back the destroy() method of Service at this time?

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.


Q: How does Service communicate with Activity?

  1. Activity calls the bindService (Intent service, ServiceConnection conn, int flags) method to get a reference to the Service object, so that the Activity can directly call the method in the Service. If we want to actively notify the Activity, we can Use the callback method
  2. Service to send messages to Activity. You can use broadcast. Of course, Activity must register the corresponding receiver. For example, if a Service wants to send the same message to multiple Activities, it is better to use this method

Q: Which system services have you used?

https://blog.csdn.net/geyunfei_/article/details/78851024


Q: Can time-consuming processing be done in Service? operate? What can be done if necessary?

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.


Q: How can AlarmManager implement timing?

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.


Q: What is the front desk service? How is it different from ordinary services? How to start a front desk service?

Foreground service is a service that is visible to users. Foreground services can be created in the form of notifications


Q: Do you understand ActivityManagerService and what role does it play?

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


Q: How to ensure that the Service is not killed?

onStartCommandIn the method, return <strong>START_STICKY</strong>

##In

StartCommand()Several constants:

  • START_STICKY The system re-creates the service and calls the
    onStartCommand() 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.
  • START_NOT_STICKY The system does not recreate the service unless there is an
    intent to be delivered. This is the safest option and prevents services from running when unnecessary.
  • START_REDELIVER_INTENT The system re-creates the service and calls the
    onStartCommand() 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 calls

ondestory(), 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.

Broadcast Receiver

Q: What are the two registration forms for broadcast? What's the difference?

There are two methods for registering broadcast receivers, namely dynamic registration in the program and static registration in the AndroidManifest file.

The characteristic of dynamically registered broadcast receivers is that when the activity used to register is turned off, the broadcast will be invalid. Static registration does not need to worry about whether the broadcast receiver is turned off. As long as the device is turned on, the broadcast receiver is also turned on. That is to say, even if the app itself is not started, the broadcast subscribed by the app will affect it when triggered.

ContentProvider

Q: How much do you know about ContentProvider?

(1) The android platform provides ContentProvider to provide the specified data set of an application to other applications. Other applications can obtain or store data from this content provider through the ContentResolver class.

(2) A content provider is only required if data needs to be shared between multiple applications. For example, address book data is used by multiple applications and must be stored in a content provider. Its benefit is to unify the way data is accessed.

(3) ContentProvider implements data sharing. ContentProvider is used to save and obtain data and make it visible to all applications. This is the only way to share data between different applications because Android does not provide a common storage area that all applications can access.

(4) Developers will not directly use objects of the ContentProvider class. Most of them implement operations on ContentProvider through ContentResolver objects.

(5) ContentProvider uses URI to uniquely identify its data set. The URI here is prefixed with content://, indicating that the data is managed by ContentProvider.

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete