Introduction to Android Development and interview 01 (Tanzhou Android Development tutorial)

Source: Internet
Author: User

Today, the teacher talked about the job market of Android, said I want to go to the interview, want to see what we can get on the market what kind of wages, do what kind of industry. I'm ashamed to say that I'm just a beginner in Android, and today I started my Android dream as well as everyone else. This is the most basic in the Tanzhou Android development tutorial.

Never thought that learning Android is for the interest, the original can also be for employment, but it is very difficult, like our Android Development video tutorial QQ Group: 175229978 inside many people, even the question is what some do not understand, but today I share in the group, Also hope to be here to help more Android developers to get started with the little friends.

1. What is activity?

(1) Brief introduction; 2. characteristics; 3. Use; 4.-à in the course of the answer remember: 1. Combine your own understanding-I think such words; 2. Combined with development experience and projects; 3. To avoid weaknesses;

One of the four components, general, a user interface corresponds to an activity

Setcontentview (),//layout to display

button.setonclicklinstener{

}

, activity is a subclass of the context, and Window.callback and Keyevent.callback are implemented to handle events that interact with the form user. Attached

2). Features

Life cycle;

3). Use;

I develop commonly used listactivity, preferenceactivity, tabacitivty, etc...

If the interface has a common feature or function, it will define itself as a baseactivity.

Display and destruction of the progress dialog box Doubanservice

4). Matters of note;

A. Run in the main thread, so can not do time-consuming operation;---"+handler Thread update interface

B. Using the life cycle to solve some problems, the activity may be destroyed in the background, save the data before destroying

Save the data in Onsaveinstancestate (bundle B), or the OnCreate (bundle B) or onrestoreinstancestate (Bundle B) method to recover the data.

    1. Please describe the activity life cycle.

(How to answer: 1. Drawing; 2. Write out a 14 0 corresponding meaning; 3. precautions;)

The life cycle describes a method that a class executes from creation (new) to Death (garbage collection).

In this process, different methods will be called for different life stages.

Activity from creation to destruction has multiple states, from one state to another, firing the appropriate callback methods, including: OnCreate OnDestroy onstop onstart onresume onpause

In fact, these methods are 22 corresponding, OnCreate creation and OnDestroy destruction;

OnStart Visible and OnStop not visible, onresume editable (i.e. focus) and onpause;

These 6 methods are relative, then only one Onrestart method is left, when is this method called?

The answer is: After the activity has been onstop, but not OnDestroy, when you start the activity again, call Onrestart (and no longer call OnCreate) method;

If it is OnDestroy, the OnCreate method is called.

Finally, the experience of the project, such as the watercress client every time you enter an interface to see the latest data, the refresh list of the operation is placed in the OnStart () method. This ensures that every time the user sees the data is up-to-date.

Multimedia playback, play to the phone. OnStop () video, video sound set to 0, record the location of the video playback mediaplayer.pause ();

OnStart () restores the scene according to the saved state. Mediaplayer.start ();

While reading the document, it was also found that the activity had two methods for the life cycle of Onpostresume () and onpostcreate (), but it was not used during development.

Onsavedinstancestate (bundle bundle) to save data before destroying; re-create Recovery data onrestoreinstancestate (bundle bundle)

3. What are some of the methods that must be performed when jumping between two activity.

In general, for example, there are two activity, called a A, a, when the B component is activated in a, a calls the OnPause () method, then B calls OnCreate (), OnStart (), Onresume (),

This time B overrides the form, and a calls the OnStop () method. If B is a transparent, or a dialog box style, the OnStop () method is not called

    1. The life cycle of the activity when the screen is switched.

Why is the activity re-created by default when the screen is switched?

Because sometimes we write our own layouts or custom controls that are dynamically implemented by code (such as the width of a control is half the screen), the system is recreated directly by default.

This life cycle is related to the configuration in the manifest file.

1, do not set the activity of the android:configchanges, the screen will recall the various life cycle

By default, the current activity is first destroyed and then reloaded

2, set the activity android:configchanges= "Orientation|keyboardhidden", the screen will not recall the various life cycle, Only the Onconfigurationchanged method is executed (so the layout can be recalculated and refreshed in this method in the case of dynamic layout)

In the game development, the screen faces are all written dead. android:orientation= "landscap" horizontal screen portraint vertical screen

5. How to set an activity to the window's style.

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

You can customize the style of an activity, see details of the Mobile defender's program details

Android:theme= "@style/floatactivity"

E:\day9\mobilesafe\res\values\style

6. What happens to your back-office activity being collected by the system? How can I save the current state before being reclaimed by the system if the activity in the background is reclaimed by the system for some reason?

In addition to the activity at the top of the stack, other activity is likely to be reclaimed by the system when the memory is low, the more the activity is at the bottom of the stack, the greater the likelihood of being recycled.

protected void Onsaveinstancestate (Bundle outstate) {

Super.onsaveinstancestate (outstate);

Outstate.putlong ("id", 1234567890);

}

Method of recovering Data callback: (2 kinds)

public void Onrestoreinstancestate (Bundle savedinstancestate)

Or

public void OnCreate (Bundle savedinstancestate) {

Determine if Savedinstancestate is empty.

If it's not empty, remove it.

Super.oncreate (savedinstancestate);

}

7. How do I exit activity? How do I safely exit a application that has called multiple activity?

Exit activity calls the finish () method directly. The user clicks the Back button to exit an activity

Exiting activity executes the OnDestroy () method.

1, throw exception forced exit:

This method makes the program force Close by throwing an exception.

Validation is possible, however, the problem that needs to be addressed is how to make the program end without ejecting the Force close window.

Secure End Process Android.os.Process.killProcess (Android.os.Process.myPid ());

2.2 The previous version can be used.

2. Record Open activity:

Each activity is opened and recorded. When you need to exit, close each activity.

List<activity> lists; In the context of the complete application

Lists = new arraylist<activity> ();

Lists.add (activity);

for (Activity activity:lists)

{

Activity.finish ();

}

3. Send specific broadcasts:

When you need to end the app, send a specific broadcast, and after each activity receives the broadcast, it's off.

To register an activity with the intention of receiving a broadcast

Registerreceiver (receiver, filter)

If you have received a broadcast that closes activity, call the finish () method to drop the current activity finish ()

4. Recursive exit

Use Startactivityforresult when opening new activity, then flag it yourself, process it in Onactivityresult, and turn it off recursively.

Above are some of the online practices.

Can actually be achieved by intent flag. Intent.setflag (flag_activity_clear_top) activates a new activity and then, in the new activity's OnCreate method, it is removed.

Tell me about your understanding of activity.

Write the points above with your own experience.

8. Does the service execute in the main thread, and does the service have time-consuming operations?

(<service

Android:process= "" ></service>)

By default, if there are no processes running for the specified service, the service and activity are running in the main thread (UI main thread) of the process where the current app is located

Service cannot perform time-consuming operations (network requests, copy databases, large files)

Executes the new thread () {}.start () in the child thread; Asynctask asynchronous task; Intentservice (Onhandleintent method takes time-consuming action)

In special cases, you can configure the process in which the service executes in the manifest file so that the service executes in another process

9. How do I pass data between two activity?

The base data type can be passed. Intent Passing Data

In the activity of a

Intent Intent = new Intent ();

Intent.putextra (name, value)

Bundle bundle = new bundle ();

Bundle.putboolean (key, value);

Intent.putextras (bundle);

Extras.putdouble (key, value)

The basic data types are passed through the intent PutExtra method

Intent i = getintent ();

I.getextras ();

Intent.getstringextra ("Key", "value");

Intent.getbooleanextra ("Key", "value")

Bundle bundle = new bundle ();

Bumdle.putshort (key, value);

Intent.putextras (Bumdle);

Intent.putextras (bundle)

--------------

Application in the global storage object, to implement their own application of this class,

Application of the basic system, each activity can be taken to

-----------------

Let the object implement the implements Serializable interface to store the object on the file.

Enables the class to implement the Serializable interface, which can then be passed through the ObjectOutputStream//object output stream

File File = new file ("C:\1.obj");

FileOutputStream fos = new FileOutputStream (file);

ObjectOutputStream oos = new ObjectOutputStream (FOS);

Student stu = new Student ();

Oos.writeobject (Stu);

Read the object out of the file

ObjectInputStream ois = new ObjectInputStream (arg0);

Student stu1 = (Student) ois.readobject ();

or implement the Parcelable interface

File/Network

Intent.setdata (Uri)

Uri.fromfile (); Transfer of large pictures

10. How do I start a service when I start an activity?

Inside the OnCreate () method of the Activity StartService ();

11. Can a different activity be placed on a different task stack in the same program?

For example, to set flag for intent when activating a new activity

Intent Flag added Flag_activity_new_task

This activated activity will be in the new task stack ...

Intent Intent = new Intent (a.this,b.class);

Intent.setflags (Intent.flag_activity_new_task);

StartActivity (Intent);

Broadcast recipient

How does the activity bind to the service, and how to start the service of the application (other people's service) in the activity? (I don't understand what you mean!) )

StartService () Once created call the method that does not have to use the service inside

Bindservice () binds the service to the caller, and if the caller is destroyed, the service destroys

Bindservice () We can use the method inside the service

Bindservice (). A way for the activity to access the service

Build a Intent object,

Intent service = new Intent (this,myservice.class);

To start a service through the Bindservice method,

Bindservice (Intent, New MyConn (), bind_auto_create);

Serviceconnection objects (overriding onserviceconnected and Onservicedisconnected methods) and Bind_auto_create.

Private class MyConn implements Serviceconnection

{

public void onserviceconnected (componentname name, IBinder service) {

TODO auto-generated Method Stub

You can use the methods inside the service through IBinder objects.

}

public void onservicedisconnected (componentname name) {

TODO auto-generated Method Stub

}

}

This is the first 13 will encounter problems, too many of my own can not digest, but I hope that we may change the operation of learning, will be better, the first blog park do not know if you can send attachments, specific I first upload QQ group: 175229978, and so I will use, send us the site.

Introduction to Android Development and interview 01 (Tanzhou Android Development tutorial)

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.