Android Interview Highlights (activity section)

Source: Internet
Author: User

A few days to go to the interview, of course, inevitably to review the next lesson, in fact, a lot of things are not particularly clear, today as a review and consolidation, I hope that this article will be to find a job of the students have some help.

1.

Q: What is activity?

Although the problem is not popular now, but still ready to prepare for it.

A:

1). Activity is one of the four components, in general, an interface with user interaction should have an activity.

2). Activity is a subclass of the context, and it implements Window.callback and Keyevent.callback, which is why people can interact with the interface. For example, we are familiar with the Click event button.setonclicklistener{}

3). I develop commonly used activity has listactivity (data display), tabactivity (production form), preferenceactivity (commonly used to write the settings interface), Sometimes when some activity has some common features or the same function, I define a baseactivity myself.

2.

Q: Please tell me about the life cycle of the activity? ( This question is quite a lot of questions).

A: We are three steps away:

The first step is to explain:

The life cycle describes the methods that a class executes from creation to extinction (note that the method that the city will execute one at a time), in which different methods are called for different life stages. Actually speaking white is a method of the implementation process.

The second step, how to execute:

The next thing we'll say is which methods are implemented:

Activity from creation to destruction has a number of States, from one state to another, the corresponding method is executed, including OnCreate ()-Create, ondestory-destroy, OnStart ()---visible, OnStop ()-invisible; onresume-get focus , OnPause---lose focus; These methods are 22 correspondence, and there is an execution that has a corresponding execution.

One thing to note here is that the popup dialog does not perform any of the above methods, because the dialog itself is part of the activity.

Finally there is a method, Onrestart (), after the activity has been onstop, but not OnDestroy, when the activity is started again to call Onrestart (and no longer call OnCreate) method; if it was destroyed. , that is to call the OnCreate () method.

The third step: Use the project you have done to explain how to use the above. For example:

Each time the client enters an interface to see the latest data, the operation of this refresh list is placed in the OnStart () method. Write the contents of the Fill data in OnStart () ok.

Do music player, need to deal with the call situation, then make a hoax to save the play breakpoint, and put the playback sound set 0, these work can be put in onstop inside do, and then to start, restore breakpoints, set the volume of these can be done in OnStart.

When we read the document, we will find the Onpostresume () and Onpostcreate () Two life cycle methods, but the development time is not used.

3.

Q: What methods do I jump between two activity?

A:

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 (), and if B covers a, we don't see a. That a also calls the OnStop method, but if B is transparent and can see a below, it will not call OnStop because only the invisible will call OnStop ();

4.

Q Talk about the life cycle of the activity in the screen switch

A: This is related to the configured manifest file.

1. If you do not set the activity's android:configchanges, each life cycle is invoked when you switch the screen, in turn:

OnPause (), OnStop (), Ondestory (), OnCreate (), Onresume (), OnStart ();

It means that you let him stop, then let it not be visible, so that it can be destroyed, and then re-create, and then go back to the focus, obtained after the display.

    1. When you set the activity's android:configchanges= "Orientation|keyboardhidden", the screen does not recall the various lifecycles, only the Onconfigurationchanged method is executed, Usually for game development, the direction is written dead.

5.

How do I set the activity as a window style?

We only need to configure in the manifest file, you can use the dialog style provided by the Android system, or you can define a style yourself in the value folder Style.xml, and then set it to the activity.

6.

Q: 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?

A: Let's see how the activity is stored: we can easily see that the more the top of the stack is not easily recycled, the system will prioritize the activity at the bottom of the stack, and to preserve the data, we need to make a replication method: Onsaveinstancestate (Bundle Outstate), believe this method everyone is not unfamiliar, because we write activity of onCreate () basically see call this method Super.oncreate (savedinstancestate); In fact, this method call is taken from the saved data: Each execution of the OnCreate method will see the bundle is not empty, not to take the data out, take the data call is Super.oncreate (savedinstancestate); This thing is common in apps that save user IDs.

Java code
    1. protected void Onsaveinstancestate (Bundle outstate) {
    2. Super.onsaveinstancestate (outstate);
    3. Outstate.putlong ("id", 1234567890);
    4. }

7.Q: How do I exit the program?

A: You see if we quit every time the program is a page down, the interface a user experience is not good, we exit can have two common methods:

    1. We write Android.os.Process.killProcess (Android.os.Process.myPid ()) by throwing an exception and then killing the abnormal thread, that is, in the code of the exception capture. This will enable the introduction of the program without the forced exit of the interface.
    2. The second is to create a new definition of an app class, the interface of the entire application, in each activity to execute the OnCreate method, the creation of activity into the global activity collection, Then you can remove the activity from the collection by clicking on the exit. The specific implementation is as follows:

A) We need to write a Myapplication.java class

Java code
    1. public class MyApplication extends application {
    2. public list<activity> activities;
    3. @Override
    4. public void OnCreate () {
    5. Super.oncreate ();
    6. Activities = new arraylist<activity> ();
    7. }
    8. }

and add activity to every oncreate.

Java code
    1. @Override
    2. protected void OnCreate (Bundle savedinstancestate) {
    3. Super.oncreate (savedinstancestate);
    4. MyApplication MyApplication = new MyApplication ();
    5. MyApplication.activities.add (this);
    6. }

The next step is to remove all activity where you want to exit:

Java code
    1. for (Activity activity:lists)
    2. {
    3. Activity.finish ();
    4. }

Today is not early, summed up a few news, hope to be helpful to everyone, continue tomorrow.

Android Interview essence (activity section)

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.