Android Face question Summary

Source: Internet
Author: User

1, talk about MVC the principle of the pattern , it's used in Android, the official Android recommendation app is developed using MVC mode. What is MVC?

MVC is an acronym for Model,view,controller, and MVC consists of three parts:

Model object: Is the main part of the application, and all business logic should be written on that layer.

View object: Is the part of the application that is responsible for generating the user interface. It is also the only layer that the user can see in the entire MVC architecture, receiving input from the user and displaying processing results.

Controller (Control) object: is based on user input, control the user interface data display and update model object state of the part of the controller more important a navigation function, want to use the user to start the phase

to model processing.

2. Android encourages weak coupling and component reuse, and the following are the specific embodiment of MVC in Android:

View layer: The general use of XML file interface description, when used can be very convenient to introduce, of course, how you learn more about Android, you can think of Android can also use javascript+ HTML and so on as the view layer, of course, there is a need for the communication between Java and JavaScript, fortunately, Android provides a very convenient communication between them implementation.

Control layer (Controller): The task of Android control layer usually falls on the shoulders of many acitvity, this phrase also implies do not write code in acitivity, to through activity delivery model business Logic layer processing, Another reason for this is that the response time of Acitivity in Android is 5s, and if time-consuming operations are put here, the program is easily recycled.

Model: The operation of the database, the operation of the network, etc. should be processed in the model, of course, business computing and other operations must be placed in the layer.
3. Life cycle of activity

Like other mobile platform applications, the life cycle of an Android application is controlled in a unified way, meaning that the application we write is in the hands of someone else (System), and we can't change it, we can only learn and adapt to 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, at this time the program will be interrupted, priority to the basic function of the service phone, in addition the system does not allow you to take up too much resources, at least to ensure that the telephone function bar, so when resources are insufficient, it is possible to 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 ();
}

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: Become interactive with the user, (in the activity stack system through the stack to manage the top of these activity, run out of pop-up stack, then back to the previous activity)

OnPause: To this step is visible but not interactive, the system will stop the animation and other CPU-consuming things from the above description already know, should be here to save some of your data, because this time your program has a lower priority, it may be recalled by the system. The data that is stored here should be in

Read it in the Onresume, note: This method takes a short time, because the next activity does not wait until this method is completed to start

OnStop: becomes invisible and is covered by the next activity

OnDestroy: This is the last method to be called before the activity is killed, perhaps the outer class calls the Finish method or the system in order to save space to temporarily kill it, you can use isfinishing () to judge it, if you have a progress dialog in the thread, please cancel it in the OnDestroy, otherwise the Cancel method called dialog will throw an exception when the threads end. Onpause,onstop, OnDestroy, in three states, the activity is likely to be killed by the system to ensure the correctness of the program, you have to write a persistent layer of code in the 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, when it is interrupted, do you want to maintain that thread, pause or kill or data rollback, right? Because Ctivity could be killed, So the variables used in the thread and some interface elements must be noticed, generally using the Android message mechanism [Handler,message] to handle multithreading and interface interaction.

4, your backstage activity is collected by the system how to do: onsaveinstancestate
When an activity A in your program runs, either actively or passively runs another new activity B at this time a will execute

Java code
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 situations, one is a is recycled, one is not recycled, the recovery of a must recall the OnCreate () method, different from the direct start is this back OnCreate () is with the parameter savedinstancestate, It's better to be onresume without being taken back. 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");
}

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

5. Please describe the data storage method of Android.

1, the use of sharedpreferences storage data;
2, file storage data;
3, SQLite database storage data;
4, the use of contentprovider storage data;
5, network storage data;

Data stores in Android are private, and other applications are inaccessible unless the data shared by other programs is obtained through Contentresolver.

6, please describe how the next contentprovider is to achieve data sharing.

A program can completely expose its own data by implementing a content provider abstraction interface, and content providers exposes data in a similar way to tables in a database. Content providers stores and retrieves data, which allows all applications to access, which is the only way to share data between applications. There are 2 ways to make your application's data public: Create a content provider of your own or add your data to a content provider that already exists, provided it has the same data type and has write content The permissions of the provider.

How can I get data exposed by other applications through a set of standard and unified interfaces? Android provides contentresolver that external programs can access the data provided by the ContentProvider via the Contentresolver interface.

7, how to enable service, how to deactivate service.

1. The first is to start by calling Context.startservice (), call Context.stopservice () to end, StartService () can pass parameters to the service

2. The second approach is to start by calling Context.bindservice (), call Context.unbindservice () to end, and access the service through Serviceconnection.
Only OnStart can be called multiple times (through multiple StartService calls) during each turn-off of the service, and the other oncreate,onbind,onunbind,ondestory can only be called once in a life cycle.

Service expansion (what is service, how to use Service,service life cycle)

Http://www.cnblogs.com/feisky/archive/2010/06/14/1758336.html

8. Please explain the relationship between message, Handler, message Queue, Looper in a single-threaded model.

Simply put, handler gets the Looper object in the current thread, Looper is used to remove the message from the MessageQueue that holds the message, and then handler to distribute and process the message.

Reference http://blog.csdn.net/xuxinyl/article/details/6097560

Http://www.dengdeng.name/u/deng/archives/2010/92.html

Android Face question Summary

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.