AD

Android interview to talk about --- the understanding of your Activity (change)

This article will involve the following content:

Activity 1 Activity of the life cycle of two to become a window: Activity Attribute Activity Set 3 your background, how is the system recovery to do: onSaveInstanceState
Four calling and being called: Our communication messenger - Intent

The life cycle of an Activity

And other applications, like mobile phone platform, Android application life cycle is controlled by a unified, that is we write the application destiny lies in someone else (system)'s hands, we can not change it, only to learn and adapt to it.

Simply put, why is this: We are running a mobile phone application, it is possible incoming phone text messages come in hair, or no electricity, and this process will be interrupted when the priority to serve the basic functions of the phone the other systems are not allow you to take up too much resources, at least to ensure that phone features bar, so the lack of resources also may be time to get rid of.

But, more seriously, Activity of the basic life cycle of 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 (); protected void onResume (); protected void onPause (); protected void onStop (); protected void onDestroy ();)

Activity will write your own override these methods as needed, onCreate is inevitable, in an Activity a normal boot process, they are called the order of onCreate -> onStart -> onResume, in the Activity was to kill the time the order is onPause -> onStop -> onDestroy, this is a complete life cycle, but it was asked, the program is running go again to call, and this program we supposed to? Bai suspended if the suspension of the time out of an Activity is a new full-screen then: onPause-> onStop, recovery time onStart-> onResume, if interrupted this application is a Theme for the Translucent or Dialog of the Activity will only be onPause, recovery time onResume.

Detail about these methods the system what to do and that we should do:

onCreate: Here to create interfaces to do some initialization data

onStart: At this point the user can see into a non-interactive

onResume: to become, and users can interact (in the activity stack system, through the stack approach in the management of these countries
Activity of the top, run the End pop-up the stack, then return to the previous Activity)

onPause: that this step is visible but can not interact, the system will stop animation and other things that consume CPU
Already know from the above description, it should be here to save some of your data, because this time
The priority of your program to reduce, there may be the system to recover. Here to save the data, should be

onResume Lane read out, pay attention: This method where the time to do a shorter, because the next
One activity does not wait until the completion of this method was to start

onstop: to become not visible, is an activity covered under the

onDestroy: This is the activity was to kill the last one is called before the method, and may be outside the class is called finish side
Law or the system in order to save space will be temporary and get rid of it, you can use isFinishing () to sub -
Off it, if you have a Progress Dialog in the thread in the rotation, please onDestroy Lane
Cancel him out, otherwise the other end of the thread, call the cancel method will throw Dialog
Abnormal.

onPause, onstop, onDestroy, three kinds of state activity are likely to be the system get rid of in order to ensure the program is correct, you have to onPause () where write operations on the persistence layer code, the user edit the contents are saved to the storage medium (usually database). In practical work because of changes in the life cycle are also many problems brought about, such as your application from a new thread in the running, this time interrupted, you have to go to maintain that thread, is to suspend or kill, or data back to roll, is not it? Activity may be killed because, so the variables used in the thread and some interface elements on the 10 million should pay attention to, and in general I are based on Android's message mechanism [Handler, Message] to handle multi-threaded and interface interaction issues. This behind me, I would mention some, most recently because of these things the first is already quite large, and so on be referred to again share my thoughts sorted out.

Activity 2 to become a window: Activity Attribute Set

Strong sense of relaxed bar, some people may want to do out of the application is the main interface, a floating in the cell phone thing, it is very simple you only need to set what could be the subject of Activity in the Activity defined in the local AndroidManifest.xml sentence:

Xml Code

android: theme = "@ android: style / Theme.Dialog"

android: theme = "@ android: style / Theme.Dialog"

This allows your application to become a pop-up dialog box, in the form of, or

Xml Code

android: theme = "@ android: style / Theme.Translucent"

android: theme = "@ android: style / Theme.Translucent"

It becomes translucent, [Friendly Tips -.-] similar to the properties of this activity can be android.R.styleable class AndroidManifestActivity method see, AndroidManifest.xml attributes of all elements of the presentations can refer to this Class android.R.styleable

The above said is the attribute name, specifically what the value is in the android.R.style can see, for example the "@ android: style / Theme.Dialog" correspond to android.R.style.Theme_Dialog ,('_' replaced by '.' "- Note: This is the article content is not smiles) can be used in describing the document, and look for the class definition and description of the file the corresponding relations are understood.

Activity 3 Your background is how to do the system recovery: onSaveInstanceState

When you program a certain Activity A in the run-time, active or passive to run another new Activity B
This time A will be the implementation of

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 will come after the completion of A, this time there are two situations, one is A has been recovered, one is not being recycled, is recycled A must re-call onCreate () method is different from the direct start is This time onCreate () where is the belt parameters
savedInstanceState, not being recovered or onResume respect enough.

savedInstanceState is a Bundle object, you basically can he understood that for the system to help you to maintain a Map object. In the onCreate () where you might use it, and if there will be a normal start onCreate it, so use the time to judge to see if it is empty.

Java code

if (savedInstanceState! = null) (
long id = savedInstanceState.getLong ( "id");
)

if (savedInstanceState! = null) (long id = savedInstanceState.getLong ( "id");)

Like the official the situation inside the Notepad tutorial, you are editing a particular note, was suddenly interrupted, then put the id of this note to remember, and then up when this id can go and take that note out, the program will complete A few . This is also your application to see what was necessary to save, such as your interface is to read a list, it does not require anything special to remember, oh, chances are you need to remember scroll location ...

Four calling and being called: Our communication messengers Intent

Intent has to say, Intent is this the intention, the application to communicate between the Intent, call La. , to telephone calls will be made La Intent , this is the essence of Android part of the loosely coupled architecture greatly increased component reuse sex, for example you want in your application, click the button to call someone, it is simple, ah, Kanxia 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);

Throw such an intention, the system saw the intention of the wake-up call to your phone dialer, playing out call. What Reading contacts, send text messages ah, e-mail, ah, were all just need to throw like intent, and this part of the design to do a very good ah.

That Intent through what to tell the system who need to accept him?
There are two methods commonly used by Intent, the first one is a direct reflection of the need to receive the code which a class is as follows:

Java code

Intent intent = new Intent (this, MyActivity.class);
intent.getExtras (). putString ( "id", "1");
tartActivity (intent);

Intent intent = new Intent (this, MyActivity.class); intent.getExtras (). PutString ( "id", "1"); tartActivity (intent);

The first method is obvious, directly designated as the recipient MyActivity, and pass some data to MyActivity, in the MyActivity where can getIntent () come to this intent and data.

The second AndroidMenifest need to look at what the configuration of the intentfilter

Xml Code

<intent-filter>
<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"
/ "
</ intent-filter>

<intent-filter> <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" /> </ intent - filter> This is used inside the configuration of the action, data, category these things, then you must think of clever intent where there will be these things, then a match not to find a recipient it?

action is actually a string name of the intent.
Above, this intent-filter configuration file shows this Activity can accept different kinds of Action, of course, is not the same as the corresponding program logic, strategy, to mention that mimeType, he is the definition of ContentProvider inside, you realize that if he knew a ContentProvider , and must specify the mimeType in order for the data used by others.

Do not know do not understand the principles that sum up one, is what you call the other new interface, that interface is not directly, but by throw an intent, allow the system to help you to call that interface, so how loose coupling ah, but in line with the life-cycle management principles by the system.

Would like to know category are all so what, Android pre-customized for you are all so what's a good action, etc., please visit the official link person Intent

ps: want to know how to call the system applications of the students and be more careful look at your logcat, each time you run a program when there is 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))
Intent look at some of the control re-set method, we know how to call the strategy, I hope you enjoy:)
标签: java code, priority, override, attribute, code java, life cycle, mobile phone, phone application, text messages, boot process, incoming phone, electricity, phone features, destiny, system recovery
分类: Mobile
时间: 2010-03-29

相关文章

  1. --- Android interview to talk about the knowledge of your Activity (change)

    This article will involve the following elements: 1 Activity of the life cycle of two to Activity into a windo ...
  2. --- Android Android Interview Dalvik introduction of virtual machine

    And the standard Java Virtual Machine Dalvik (JVM) The primary difference between the one that Dalvik register ...
  3. (Transfer) Android Interview --- talk about your understanding of Activity (Interview will be asked.)

    This article will relate to the following content Activity of the life cycle of a second to become a window Ac ...
  4. Android Interview Activity --- talk about your understanding of (the interview will be asked)

    This article will relate to the following content Activity of the life cycle of a second to become a window Ac ...
  5. android interview questions

    View redraw and memory leak seems to be a question often asked interview 1. View the refresh: Where the need t ...
  6. android file system structure and initial understanding of the boot process (Reprinted)

    1, android file system structure android source code compiled by system.img, ramdisk.img, userdata.img image f ...
  7. (Transfer) Android Interview ---- Android system architecture diagrams and simple system architecture description

    System architecture and its Android operating systems, the use of a hierarchical structure. From the chart to ...
  8. Android interview question, the source code to achieve translucent popupwindow

    1. To set the translucency topic 2. Set the alpha value of window / / WindowManager.LayoutParams lp = getWindo ...
  9. Chapter I: Android entering the door (different data transmission between the Activity)

    Effect To set the first Activity main.xml <?xml version="1.0" encoding="utf-8"?> < ...
  10. Android basic control of the Dialog (b) Dialog theme Activity

    Transfer from: http://www.cnblogs.com/xirihanlin/archive/2009/08/04/1538571.html , reproduced please explain, ...
  11. SOLR in Schema.xml the filedType understanding of some properties [change] Notes up

    from matching across instances. 4. OmitNorms What is the Norm, now do not know? ? ? ? 5. LowerCaseFilterFactor ...
  12. Android Development notes 5: How to set a full-screen Activity

    Sometimes we hope that our interface is full screen, how to do? Is very simple methods in the Activity of onCr ...
  13. android Activity description

    Activity in android development is very important, in an application, each display screen is a Activity. So le ...
  14. classic android face questions

    Android classic interview questions 1, Android dvm process and the Linux process, the application process is t ...
  15. Beginners Android (change)

    Android Beginners Android Process Lifecycle Android system memory is low, you need to use the old application ...
  16. Introduction android development (change)

    Environmental structures Before you begin, you need to prepare the following environmental and procedural Nece ...
  17. Android Activity type application skills sharing

    Android Activity class in the Android operating system, applications are often important. So how can the right ...
  18. Several layout android

    LinearLayout orientation: the direction of the specified control, respectively, horizontal (level), vertical ( ...
  19. Threading model of Android

    1 Introduction The term refers to the original meaning of the robot Android, Google in November 2007 issued to ...