Android Common interview topics

Source: Internet
Author: User

Life cycle of 1.Activity

Transferred from: http://kb.cnblogs.com/page/70125/

Thank you landlord to share

First look at the activity life cycle diagram provided in the Android API (not clear, you can see the full article, looking back at this diagram, you will understand):

The activity is actually inherited applicationcontext This class, we can rewrite the following method, the following code:

View Plaincopy to Clipboardprint?
public class Activity extends ApplicationContext {
protected void OnCreate (Bundle savedinstancestate);
protected void OnStart ();
protected void Onrestart ();
protected void Onresume ();
protected void OnPause ();
protected void onStop ();
protected void OnDestroy ();
}

In order to facilitate the better understanding of everyone, I simply wrote a demo, do not understand the activity cycle of friends, you can practice, everyone follow my steps.

First step: Create a new Android project, which I named Activitydemo.

Step Two: Modify Activitydemo.java (I have re-written the above seven methods, mainly with log printing), the code is as follows:

Package Com.tutor.activitydemo;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.Log;
public class Activitydemo extends Activity {
private static final String TAG = "Activitydemo";
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
LOG.E (TAG, "Start oncreate~~~");
}
@Override
protected void OnStart () {
Super.onstart ();
LOG.E (TAG, "Start onstart~~~");
}
@Override
protected void Onrestart () {
Super.onrestart ();
LOG.E (TAG, "Start onrestart~~~");
}
@Override
protected void Onresume () {
Super.onresume ();
LOG.E (TAG, "Start onresume~~~");
}
@Override
protected void OnPause () {
Super.onpause ();
LOG.E (TAG, "Start onpause~~~");
}
@Override
protected void OnStop () {
Super.onstop ();
LOG.E (TAG, "Start onstop~~~");
}
@Override
protected void OnDestroy () {
Super.ondestroy ();
LOG.E (TAG, "Start ondestroy~~~");
}
}

Step three: Run the above project as follows (nothing special):

Core in Logcat window, if you do not use logcat you can take a look at my article Log text (LOG.V,LOG.D,LOG.I,LOG.W,LOG.E), we opened the application has been executed OnCreate () OnStart ()->onresume three methods, take a look at the Logcat window as follows:

Back key:

When we press the back key, our application will end, and this time we will call OnPause ()->onstop ()->ondestory () Three methods as shown:

Home key:

When we opened the app, like a browser, I was browsing the NBA news, and when I saw half of it, I suddenly wanted to listen to the song, and then we chose to press the home button to open the Music app, and when we pressed home, the activity executed OnPause (). OnStop () These two methods, the application is not destroyed at this time. As shown in the following:

When we start the Activitydemo application again, the Onrestart ()->onstart ()->onresume () Three methods are executed separately, as shown in:

Here we will draw a question, when we press the Home button, and then enter the Activitydemo application, our application status should be the same as the state before the home button, also for the convenience of understanding, here I will activitydemo code to make some changes, is to add a edittext.

Fourth step: Modify the Main.xml layout file (add a edittext), the code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
>
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"
/>
<edittext
Android:id= "@+id/edittext"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
/>
</LinearLayout>

Fifth step: Then the other unchanged, run the Activitydemo program, enter in the EditText such as "Frankie" string (such as:)

At this time, you can press the Home button, and then start the Activitydemo application again, this time there is no edittext we entered the word "Frankie", such as:

This obviously cannot be called a qualified application, so we need to implement it ourselves in several methods of activity, as shown in the sixth step below:

The sixth step is to modify the Activitydemo.java code as follows:

Package Com.tutor.activitydemo;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.widget.EditText;
public class Activitydemo extends Activity {
private static final String TAG = "Activitydemo";
Private EditText Medittext;
Defines a string type used to access the value of our edittext input
Private String mstring;
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Medittext = (EditText) Findviewbyid (R.id.edittext);
LOG.E (TAG, "Start oncreate~~~");
}
@Override
protected void OnStart () {
Super.onstart ();
LOG.E (TAG, "Start onstart~~~");
}
When you press the home key and then start the app again, we want to restore the previous state
@Override
protected void Onrestart () {
Super.onrestart ();
Medittext.settext (mstring);
LOG.E (TAG, "Start onrestart~~~");
}
@Override
protected void Onresume () {
Super.onresume ();
LOG.E (TAG, "Start onresume~~~");
}
When we press the home key, I assign the input value to mstring in the OnPause method
@Override
protected void OnPause () {
Super.onpause ();
mstring = Medittext.gettext (). toString ();
LOG.E (TAG, "Start onpause~~~");
}
@Override
protected void OnStop () {
Super.onstop ();
LOG.E (TAG, "Start onstop~~~");
}

@Override
protected void OnDestroy () {
Super.ondestroy ();
LOG.E (TAG, "Start ondestroy~~~");
}
}

Seventh step: Rerun the Activitydemo program, repeat the fifth step, when we press the home key, when the application is launched again, EditText has the last input "Frankie" word, as shown:

Four startup modes in 2.activity

Transferred from: http://www.androidchina.net/3173.html

Getting Started with Android: Activity four startup modes

First, the introduction of the starting mode

The startup mode is simply that the activity starts with the policy, the Android:launchmode property of the label in the Androidmanifest.xml is set;

The starting mode has 4 kinds, namely standard, Singletop, Singletask, singleinstance;

1, for the use of standard mode of activity, the system does not care whether the activity has been in the return stack exists, each start will create a new instance of the activity. For example a start a,a and then start a,a continue to start a, and then separately out of the stack, 2, when the active startup mode is specified as Singletop, when the activity is started when the stack top is found to be the activity, you think you can use it directly, no longer create a new activity instance.
3, when the active startup mode is specified as Singletask, each time the activity is started, the system will first check the return stack for the existence of an instance of the activity, if the discovery already exists then directly use the instance, and put all activities above the activity out of the stack, if not found will create a new activity instance.
4, using the singleinstance mode can solve this problem, in this mode there will be a separate return stack to manage the activity, regardless of which application to access the activity, the same return stack shared, also resolves the problem of shared activity instances. Suppose B starts a,a start C, where A's start mode is SingleInstance, then: the page order returned is C-b-a

Application of Singtask:

can be used to exit the entire application.

Set the main activity to Singtask mode, then transfer the activity to the main activity in order to exit, then rewrite the onnewintent function of the main activity and add a finish to the function.

Report:

To exit a single activity method:

Call Finish

Kill the Process: KillProcess (Process.mid)

To terminate a running virtual machine: System.exit ()

Exit the entire application:

Manufacturing throw exception causes the entire program to exit

Put all the activity into a list and then drop all the activity,finish when you need to exit.

To complete the Exit function by broadcasting

The completion of the Exit function via broadcast is accomplished by registering a broadcast receiver with the activity at each activity creation (OnCreate) and sending the broadcast when exiting. The approximate code is as follows:

@Overrideprotected void OnCreate (Bundle savedinstancestate) {       super.oncreate (savedinstancestate);       Intentfilter filter = new Intentfilter ();       Filter.addaction ("Finish");       Registerreceiver (mfinishreceiver, filter);       ......} Private Broadcastreceiver Mfinishreceiver = new Broadcastreceiver () {    @Override public    void OnReceive (Context Context, Intent Intent) {           if ("Finish". Equals (Intent.getaction ())) {              log.e ("#########", "I am" + Getlocalclassname ()                     + ", now finishing myself ...");              Finish ();}}    ;

write the above code in a base class, so that you call close at the point where you want to exit, send the broadcast, and each activity receives the broadcast, it will finish off .

3. Talk about the common design patterns and scenarios used in Android

4. How to get the height of a button

When the button is initially obtained, it is not the height, width, or coordinate value, which needs to be obtained by the way of delay, the code is as follows:

Define global button pairs like

Private Button bt1;

And then inside the OnCreate: Create a thread for synchronization

1 Setcontentview (R.layout.main); 2   3BT1 =(Button) Findviewbyid (R.ID.BUTTON01); 4 NewThread ()5      {  6 @Override7            Public voidRun ()8           {  9          synchronized( This)  Ten          {   One           Try   A           {   -Wait (1000);//1 seconds -           }   the           Catch(interruptedexception e) -           {   -            //TODO auto-generated Catch block - E.printstacktrace ();  +           }   -          }   +LOG.I ("Test", "Height:" +bt1.getheight ());  ALOG.I ("Test", "width:" +bt1.getwidth ());  atLOG.I ("Test", "Coordinate x:" +bt1.getright ());  -LOG.I ("Test", "Coordinate y:" +bt1.gettop ());  -           }   -}.start ();
Viewtreeobserver vto = Imageview.getviewtreeobserver ();   Vto.addongloballayoutlistener (New Ongloballayoutlistener () {     @Override public       void Ongloballayout () {         Imageview.getviewtreeobserver (). Removeglobalonlayoutlistener (this);         Imageview.getheight ();        Imageview.getwidth ();    }   });   


This method is basically the same as the 2nd method, but he is the global layout change listener, so it is most recommended to use.

5. The process of customizing the view

How to detect memory leaks in 6.android

7. What is the difference between the start and run methods of threading thread? Do you have to call the Start method if you want to open a thread?

8. How to learn the knowledge about Android

Android Common interview topics

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.