Android face Test

Source: Internet
Author: User

View redraw and memory leaks seem to be a frequently asked question in the interview.
1. Refresh of view:
Where it needs to be refreshed, send the message using Handle.sendmessage and then execute invaliate or postinvaliate in the getmessage of handle.
2. GC Memory leaks
The situation arises:
1. The cursor of the database is not closed
2. When constructing adapter, the cache is not used Contentview
Optimization of derived listview--Reduce the creation of view objects, fully use Contentview, you can use a static class to optimize the process of processing getview/
3.Bitmap object uses recycle () to free memory when not in use
Objects in 4.activity have a life cycle greater than activity
Debug method: Ddms==> heapszie==>dataobject==>[total Size]
There are other questions that you are welcome to raise, which can be the overall architecture and this HAL layer.
This article will cover the following sections
The life cycle of an activity
Two-let activity into a window: Activity property settings
What do you do when your backstage activity is recovered by the system?: Onsaveinstancestate
Four calls and is called: our Messenger of communications –intent
The life cycle of an activity

Like apps from other mobile platforms, the life cycle of Android apps is controlled in a unified and
That is, we write the application fate in the hands of others (systems), we can not change it, can only learn and
Adapt it.
Briefly explain why this is the case: when we run an app on our phone, it's possible to call in
Send in text messages, or no electricity, this time the program will be interrupted, priority to the basic function of the service phone, another
The external system also does not allow you to occupy too much resources, at least to ensure that the telephone function bar, so when resources are insufficient, there is a
can be killed.
To the end, the basic life cycle of activity is shown in the following code:
Java Code

Public
Class MyActivity extends Activity {
Protected
void OnCreate (Bundle savedinstancestate);
Protected
void OnStart ();
Protected
void Onresume ();
Protected
void OnPause ();
Protected
void OnStop ();
Protected
void OnDestroy ();
}
public class MyActivity extends Activity {protected void OnCreate (Bundle savedinstancestate); protected void OnStart (); p rotected void Onresume (); protected void OnPause (); protected void onStop (); protected void OnDestroy (); }
You write your own activity will overload these methods as needed, onCreate is unavoidable, in an activity normal start process, they are called in the order of OnCreate-OnStart-Onresume, When the activity was killed in the order is OnPause-onStop--and OnDestroy, this is a complete life cycle, but someone asked, the program is running to call, this program do? If the abort is a new activity is full screen then: onpause->onstop, Recovery time onstart->onresume, if interrupt this application is a theme for translucent Or dialog activity so just OnPause, recovery time onresume.
Explain in detail what the system is doing and what we should do in these methods:
OnCreate: Create the interface here and do some initialization work on the data
OnStart: To this step becomes user visible non-interactive
Onresume: To become interactive with the user, (in the activity stack system through the way of the stack to manage these
Top of activity, run out of pop-up stack, then go back to previous activity)
OnPause: To this step is visible but not interactive, the system will stop the animation and other CPU-consuming things
As already known from the above description, you should save some of your data here, because at this time
Your program's priority is reduced and may be withdrawn by the system. The data that is stored here should be in
Onresume read it out, note: This method is done in short time, because the next
Activity will not be activated until this method is completed.
OnStop: becomes invisible and is covered by the next activity
OnDestroy: This is the last method that was called before the activity was killed, possibly the outer class called the finish side
method or system to save space and temporarily kill it, you can use isfinishing () to judge
Break it, if you have a progress dialog in the thread, go in OnDestroy
Cancel him off, or else when the thread ends, the Cancel method that calls dialog will throw
The exception.

Onpause,onstop, OnDestroy, in three states, the activity could be killed by the system.
In order to ensure the correctness of the program, you have to write the persistent layer code in OnPause () to save the user's edits to the storage media (usually the database). There are a lot of problems in the actual work because of the change of life cycle, such as when your application has a new thread running, it breaks down, you still have to maintain that thread, whether it's pausing or killing or data rollback, right? Because the activity could be killed, So the variables used in the thread and some interface elements must be noticed, and I usually use the Android messaging mechanism [Handler,message] to deal with multithreading and interface interaction issues. I will talk about this later, because these things have a very big head, so I have to clarify my thoughts and then share with you.
Two-let activity into a window: Activity property settings

Talk a little easier, maybe someone wants to make the application is a floating in the main interface of the phone things, then very
Simple you just need to set the subject of the activity to define the activity in the Androidmanifest.xml.
Local sentence:

XML code

Android:theme= "@android: Style/theme.dialog"
Android:theme= "@android: Style/theme.dialog"

This causes your application to pop up as a dialog box, or

XML code

Android:theme= "@android: Style/theme.translucent"
Android:theme= "@android: Style/theme.translucent"
It becomes translucent, [friendly tip-.-] similar to the properties of this activity can be on Android. As seen in the Androidmanifestactivity method of the R.styleable class, the properties of all elements in Androidmanifest.xml can be referenced in this class android.r.styleable
It says the attribute name, what the value is on Android. As you can see in R.style, this "@android: Style/theme.dialog" corresponds to Android. R.style.theme_dialog, (' _ ' replaced with '. ' <--Note: This is the article content is not a smiley face) can be used in the description file, find the class definition and description of the corresponding relationship in the file is understood.

What do you do when your backstage activity is recovered by the system?: Onsaveinstancestate

When an activity a in your program is running, active or passive runs another new activity B
At this point, a will execute
Java code

Public
void Onsaveinstancestate (Bundle outstate) {
Super.onsaveinstancestate (outstate);
Outstate.putlong ("id", 1234567890);
}
public void Onsaveinstancestate (Bundle outstate) {super.onsaveinstancestate (outstate); Outstate.putlong ("id", 1234567890);}

B after completion will come to find a, this time there are two cases, one is a is recycled, one is not recycled, was returned
A must recall the OnCreate () method, different from the direct start is this back OnCreate () is the parameter
Savedinstancestate, not to be retracted is still onresume just fine.
Savedinstancestate is a bundle object, you can basically understand him as a system to help you maintain a map object. In OnCreate () you may use it, if the normal start oncreate will not have it, so use to determine whether it is empty.
Java code

if (savedinstancestate! = null) {
Long id = savedinstancestate.getlong ("id");
}
if (savedinstancestate! = NULL) {Long id = savedinstancestate.getlong ("id");}

Just like in the official Notepad tutorial, you are editing a note that is suddenly interrupted, then remember the ID of this note, and when you get up, you can take that note out with that ID, and the program is complete. This is also to see your application need not to save anything, such as your interface is to read a list, it does not need to remember anything special, oh, maybe you need to remember the position of the scroll bar ...
Four calls and is called: our Messenger of communications intent
To say intent, intent is this intention, the application between the intent to communicate, make a phone call, to a
Phone intent, this is the essence of the loose coupling of Android architecture, greatly improve the reusability of the components, such as you want to click on the button in your application, call someone, very simple ah, look at the code first:
Java code

Intent Intent = new Intent ();
Intent.setaction (Intent.action_call);
Intent.setdata (Uri.parse ("Tel:" + number));
StartActivity (Intent);
Intent Intent = new Intent (); Intent.setaction (Intent.action_call); Intent.setdata (Uri.parse ("Tel:" + number)); StartActivity (Intent);
Throwing out such an intent, the system sees your intentions to wake up the phone dialer and make a call. What to read contact, texting Ah, mail ah, all just need to throw out intent, this part of the design is really good ah.
What does the intent tell the system who needs to accept him?
There are two ways to use intent, the first of which is to directly indicate which class is required to receive the code as follows:
Java code

Intent Intent = new Intent (this, myactivity.class);
Intent.getextras (). putstring ("id", "1");
StartActivity (Intent);
Intent Intent = new Intent (this, myactivity.class), Intent.getextras (). putstring ("id", "1"); tartactivity (Intent);

The first way is obvious, the direct designation of MyActivity as the recipient, and some data to myactivity, in myactivity can use Getintent () to this intent and data.
The second one needs to look at the configuration of the Intentfilter in Androidmenifest.
XML code


<action
Android:name= "Android.intent.action.VIEW"
/>
<action
Android:value= "Android.intent.action.EDIT"
/>
<action
Android:value= "Android.intent.action.PICK"
/>
<category
Android:name= "Android.intent.category.DEFAULT"
/>
<data
Android:mimetype= "Vnd.android.cursor.dir/vnd.google.note"
/>

This configuration uses the action, data, category these things, so smart you must think intent will have these things, and then a match do not find the receiver?
An action is actually a string name of intent.
The above Intent-filter configuration file shows that the activity can accept different Action, of course, the corresponding program logic is not the same, mention the MimeType, he is defined in ContentProvider, If you realize a contentprovider yourself, you have to specify MimeType in order for the data to be used by others.
Do not know the principle of white, summed up a sentence, is that you call other interface is not directly new that interface, but by throwing a intent, let the system help you to call that interface, so how loose coupling ah, and in line with the life Cycle system management principles.
Want to know what the category is, Android for you pre-custom action all have what, and so on, please personally visit the official link intent
PS: Want to know how to call the system application of the classmate, you can take a closer look at your logcat, each time you run a program is not some information such as:
Starting activity:intent {Action=android.intent.action.maincategories={android.intent.category.launcher} flags= 0x10200000comp={com.android.camera/com.android.camera.gallerypicker}}
A comparison of some of the intent set method, you know how to call, I hope you like:

One, how do you optimize the ListView.
Two, the view of the refresh, said before
Third, IPC and principle
Quad, Android multithreading
Five, why Android to design 4 large components, their connection, not design line (mainly to implement the MVC pattern, but the most difficult mode in Java is this, very few products can make this model very good "Technicolor interviewer asked this")
Six, service cycle, activity cycle, talk about your Android internal applications, such as his phone calls, as well as contacts and other applications. The framework layer has a lot of things to look at, familiar with the Android how to do, whether you do application development or application framework layer development is very good.
In is your project experience, highlight what difficulties you encounter, and then how to solve! As much as possible to highlight each technology point, of course, the interviewer sometimes in order to reflect whether you have actually done, he will ask you, you in this application to do that module, the number of classes and other problems.
Occasionally, the interviewer will ask you, have you ever used a unit test that comes with Android?
Of course, I interviewed a lot of units, some do tablet, mobile phone, digital TV, have to make a client such as ERP, and so on, out of the previous three, basically all will be android to get rid of, if you really want to do Android, we have to learn a lot.
In short, a word, what kind of interviewer have, go to the interview to do all the psychological preparation, whether the technology or the foundation are solid. A person's ability to talk is also very important, in short, is not very standard Mandarin, at least you say to let others understand, and the interviewer to speak very thoroughly, so you get the opportunity to offer more, talk about wages also have advantages ~ ~ Of course, once a company's interviewer told me that technology is at the expense, as long as you have the ability , how much money he has to please.
Indeed, during the job hunt in Beijing, the cattle are really a lot, and some interviewers are very good, give a lot of loyalty to the views. Not every interviewer wants to embarrass you the most important or want to know your technology, because they also eat company Rice, have to be responsible for this.

1.View How to refresh?
What is the difference between 2.DDMS and TraceView?
What if the 3.activity is recycled?
4. How to introduce C language in Java?
Answer
1.View can call invalidate () and Postinvalidate () both methods to refresh
2.DDMS is a program execution viewer, where you can see information such as threads and stacks, TraceView is a program Performance analyzer
3.activity recycled, that's just another one.
4.java Call C language Program, can use JNI interface to implement
The above answer is only for reference, after all, my personal ability is limited, will inevitably answer the wrong, hehe ...
Answer:
1.View is refreshed by the system (there is a loop inside the system, monitoring events, doing business processing, drawing the UI), you can use Postinvalidate () to drive the system to refresh.
2. (I don't know)
3. Please refer to the activity life cycle, if the system destroy, that is, the recovery, only from the new start
4. Call through JNI. It is recommended to read the Java Native Interface Programmer's Guide and Specification, the English version, from the Sun website.

Interview manuals from IT companies

Android face Test

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.