Android interview questions -- Activity, android -- activity

Source: Internet
Author: User

Android interview questions -- Activity, android -- activity

1,What is Activity?
Activity is one of the four most basic and common components in Android (Activity, Service, Content Provider, BroadcastReceiver broadcast receiver.

Activity is an application component that provides a screen for users to interact with each other to complete a task. All operations in the Activity are closely related to the user. It is a component responsible for interacting with the user. You can use setContentView to display the specified control. In an android app, an Activity is usually a single screen, which can display some controls or listen to and process user events to respond. Activities communicate with each other through Intent. Frequently Used Products include FragmentActivitiyListActivity, PreferenceActivity, TabAcitivty, etc... For more information, see http://www.cnblogs.com/wuyudong/p/5959056.html.

2,Please describe the Activity Lifecycle

Let's talk about these processes.

1. Start Activity: the system first calls the onCreate method, then calls the onStart method, and finally calls onResume. The Activity enters the running state.

2. The current Activity is overwritten by another Activity or locked: the system will call the onPause method to pause the execution of the current Activity.

3. The current Activity is overwritten to the front-end or unlock screen: The system calls the onResume method and enters the running status again.

4. Switch the current Activity to the new Activity interface or press the Home Key to return to the main screen and return to the background: the system will first call the onPause method and then call the onStop method to enter the stuck state.

5. the user returns to this Activity: the system will first call the onRestart method, then call the onStart method, and finally call the onResume method to enter the running status again.

6. the current Activity is in the overwriting status or the background is invisible, that is, steps 1 and 2. If the system memory is insufficient, the current Activity is killed and the user returns to the current Activity: call the onCreate method, onStart method, and onResume method again to enter the running status.

7. Exit the current Activity: the system first calls the onPause method, then calls the onStop method, and finally calls the onDestory method to end the current Activity.

There are multiple States for an Activity from creation to destruction. The corresponding callback methods will be triggered when the Activity is from one State to another. These callback methods include: onCreate onStart onResume onPause onStop onDestroy in fact, these methods correspond to each other, onCreate and onDestroy destroy; onStart and onStop are invisible; onResume can be edited (I .e. focus) and onPause; common Activity types include FragmentActivitiy, ListActivity, and TabAcitivty.

4,How to save the Activity status?

Normally, the system automatically saves the Activity status. This function is required only when additional data needs to be saved. Generally, the activity instance still exists in the memory after the onPause () and onStop () methods are called, and all the information and status data of the activity will not disappear. When the activity returns to the foreground, all changes are retained. However, when the system memory is insufficient, the activity that calls the onPause () and onStop () methods may be destroyed by the system, and the instance objects of the activity will not exist in the memory. If the activity returns to the foreground, the previous changes will disappear. To avoid this, we can override the onSaveInstanceState () method. The onSaveInstanceState () method accepts a Bundle type parameter. developers can store status data to this Bundle object, so that even if the activity is destroyed by the system, when the user restarts the activity and calls its onCreate () method, the Bundle object above will be passed as a real parameter to the onCreate () method, developers can retrieve the stored data from the Bundle object, and then use the data to restore the activity to the State before it is destroyed. Note that the onSaveInstanceState () method is not always called, because in some scenarios, you do not need to save status data. for example, when you press the BACK key to exit the activity, the user obviously wants to close the activity. At this time, there is no need to store data for the next restoration, that is, onSaveInstanceState () method cannot be called. if you call the onSaveInstanceState () method, the call will occur before the onPause () or onStop () method.

@Overrideprotected void onSaveInstanceState(Bundle outState) {// TODO Auto-generated method stubsuper.onSaveInstanceState(outState);}

5,Which of the following methods must be executed when two activities are redirected?

Generally, for example, there are two activities called A and B. When activating component B in A, A will call the onPause () method, and B will call onCreate (), onStart (), onResume ().
At this time, B overwrites the form, and A will call the onStop () method. If B is transparent or A dialog box style, the onStop () method of A will not be called.

6,Lifecycle of the Activity during horizontal/vertical screen Switching
The lifecycle is related to the configuration in the configuration file.
1. When the android: configChanges of an Activity is not set, the current activity will be destroyed and then reloaded by default when the screen is switched to the Activity.
2. When Activity android: configChanges = "orientation | keyboardHidden | screenSize" is set, the function of switching the screen will not re-call each lifecycle, but will only execute the onConfigurationChanged method. Usually in game development, the screen orientation is completely written.

7,How to Set an Activity as a window style
You only need to configure the following attributes for our Activity. Android: theme = "@ android: style/Theme. Dialog"

8,How to exit the Activity? How to safely exitApplication?

(1) generally, you only need to press the return key to exit an Activity. We can write code to exit the activity and directly call the finish () method.

(2) record the opened Activity: every time an Activity is opened, it is recorded. When you need to exit, close every Activity.

// Pseudo-code List <Activity> lists; // in the global variables of the application, lists = new ArrayList <Activity> (); lists. add (this); for (Activity activity: lists) {activity. finish ();} lists. remove (this );

(3) send specific broadcasts:

Send a specific broadcast when you need to end the application. After each Activity receives the broadcast, close it.

// Register an activity with the intent of accepting the broadcast. The registerReceiver (filter) // if the activity is disabled, call finish () method To finish () The current activity

(4) recursive exit

Use startActivityForResult when opening a new Activity, add a flag on your own, process it in onActivityResult, and disable it recursively.

(5) intent. setFlags (intent. FLAG_ACTIVITY_CLEAR_TOP) can be used to activate a new activity. If this Activity already exists in the task stack, the system will kill all the activities on this Activity. In fact, the startup mode configured for the Activity is SingleTop.

9,What are the differences between singletop and singletask in the four startup modes of Activity?Generally, the usage mode of bookmarks is singletop. Why not use singletask?

SingleTop is similar to standard mode. The only difference is that when the jump object is the activity at the top of the stack (which should be understood as the activity visible to the user), the program will not generate a new activity instance, instead, it directly jumps to the activity instance that exists at the top of the stack. For example, when Act1 is in singleTop mode

There is still only one instance in the back stack. If you press the return key, the program will exit directly. SingleTask mode and singleInstance mode both create only one instance. In this mode
Like the activity at the top of the stack, the program will not generate a new instance (the premise is that the instance already exists in the stack ). This mode is quite useful. In the future multi-activity development, multiple instances will be generated on the same page due to the jump relationship, which is always a little poor in user experience, if you declare the corresponding activity as the singleTask mode, this problem will no longer exist. Activities on the home page are frequently used.

10,What is the difference between Context, Activity, and Appliction in Android?
Same: both Activity and Application are child classes of Context. Context literally refers to the meaning of Context. In actual applications, Context also applies to various parameters in the management Context.
The total use of numbers and variables allows us to easily access various resources. Different: Maintenance lifecycles are different. Context maintains the lifecycle of the current Activity, and Application maintains
The entire project lifecycle. When using context, be careful with Memory leakage to prevent memory leakage. Pay attention to the following aspects:
1. Do not make an object with a long life cycle reference the activity context. That is, ensure that the object that references the activity has the same life cycle as the activity itself.
2. For objects with a long life cycle, you can use application and context.
3. Avoid non-static internal classes and try to use static classes to avoid lifecycle issues. Pay attention to the lifecycle changes caused by internal class references to external objects.

11,Data is transmitted between two activities, except intent, broadcast receiver, contentWhat else does provider have?
1) use static data and public static member variables
2) using external storage for transmission,
For example, File storage
SharedPreferences preferences
Sqlite Database

12,What is Context?
1. It describes the information of an application environment, that is, the context.
2. This class is an abstract class. Android provides the concrete implementation class (ContextIml) of this abstract class ).
3. We can use it to obtain application resources and classes, as well as some application-level operations, such as starting an Activity, sending broadcasts, accepting Intent, and information.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.