Android interview essence (Activity part), Android activity
My younger brother will go to the interview in a few days. Of course, I have to review my lessons carefully. In fact, many things are not very clear. Today we will review and consolidate them, I hope this article will help my students looking for a job.
1.
Q: What is activity?
Although this question is not popular now, we are still prepared for it.
A:
1). activity is one of the four main components. Generally, an interactive interface corresponds to an activity.
2) Activity is a subclass of Context and implements Window. callback and keyEvent. callback. In this way, people can interact with the interface. For example, we are familiar with the click event Button. setOnClickListener {}
3 ). commonly used activities I developed include listActivity, TabActivity, and PreferenceActivity ), sometimes some activities have some common features or functions, I will define a BaseActivity by myself.
2.
Q:Please talk aboutWhat is the lifecycle of an Activity?().
A: Let's take three steps:
Step 1:
The life cycle describes the methods that a class will execute from creation to extinction (Note: The methods that will be executed one by one in the city). In this process, different methods are called for different stages of life. To put it bluntly, it is the execution process of Methods one by one.
Step 2: how to execute:
Next, let's talk about the methods that have been executed:
Activity has multiple States from creation to destruction. from one state to another, corresponding methods will be executed, including Oncreate ()-create, OnDestory-destroy; OnStart () --- visible, onStop ()-invisible; OnResume-obtains the focus, OnPause --- loses the focus; these methods correspond to each other, and one execution must have a corresponding execution.
Note that the pop-up dialog box does not execute any of the above methods, because the dialog box itself is part of the Activity.
There is another method, OnRestart (). After the Activity is onStop but not onDestroy, The onRestart (instead of onCreate) method is called when the Activity is started again; if the object is destroyed, the onCreate () method is called.
Step 3: Use your own project to describe how to use it. For example:
Each time the client enters an interface, it will see the latest data. This refresh list operation will be placed in the onStart () method. in onStart (), it is OK to write the data filling content.
When you are playing a music player, you need to handle incoming calls. In this case, you can save the playback breakpoint and set the playing sound to 0. This can be done in OnStop, then, when starting, restore the breakpoint and set the volume, which can be done in OnStart.
When reading the document, we will find the onPostResume () and OnPostCreate () Life Cycle methods, but they are not used during development.
3.
Q: How does one redirect between two activities?
A:
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 (), if B overwrites A, we will not see A, then A will call the OnStop method, but if B is transparent, we can also see that OnStop is not called if A is under it, Because OnStop () is called only when it is unavailable ();
4.
Q: What is the life cycle of the Activity during screen switching?
A: This is related to the configuration list file.
1. If the android: configChanges of the Activity is not set, each lifecycle will be called during screen switching, which is:
OnPause (), onStop (), OnDestory (), onCreate (), OnResume (), onStart ();
This means that you can stop it first, and then make it invisible, so that it can be destroyed, and then re-created, and then go back to the focus, and display it after obtaining it.
5.
How do I set Activity to window style?
You only need to configure the style in the configuration file. You can use the dialog style provided by the Android system or the style in the value folder. xml defines a style and then sets it to Activity.
6.
Q: What should I do if my background Activity is recycled by the system? If the background Activity can be recycled by the system for some reason, how can we save the current status before being recycled by the system?
A: Let's take A look at how the Activity is stored: We can easily see that the more at the top of the stack is not easily recycled, the system will first recycle the activity at the bottom of the stack. To save the data, we need to re-write a method: onSaveInstanceState (Bundle outState). I believe this method is familiar to everyone, because we have basically seen calling this method in onCreate () of Activity. onCreate (savedInstanceState); in fact, this method is called from the saved data: every time you execute the oncreate method, it will check whether the Bundle is empty, not to retrieve the data, data Retrieval calls super. onCreate (savedInstanceState); this is common in applications that store user IDs.
Java code
- Protected void onSaveInstanceState (Bundle outState ){
- Super. onSaveInstanceState (outState );
- OutState. putLong ("id", 1234567890 );
- }
7. Q: How do I exit the program?
A: You can see that if every time we exit the program, there will be one page, and the user experience on the interface will be poor. There are two common methods to exit:
A) We need to write a MyApplication. java class.
Java code
- Public class MyApplication extends Application {
- Public List <Activity> activities;
- @ Override
- Public void onCreate (){
- Super. onCreate ();
- Activities = new ArrayList <Activity> ();
- }
- }
Then add the activity in each OnCreate
Java code
- @ Override
- Protected void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- MyApplication myApplication = new MyApplication ();
- MyApplication. activities. add (this );
- }
The next step is to remove all activities from the exit:
Java code
- For (Activity activity: lists)
- {
- Activity. finish ();
- }
It is not early today. I have summarized several messages, hoping to help you and continue tomorrow.