12th-day Android Activity Learning 1

Source: Internet
Author: User



Activity is usually the first class encountered when learning Android to write a demo. It is well-known and comparable to the classic print "Hello World, after all, in Android, even if you want to display a "Hello World" on the interface, you must first create a Activiy subclass to create a view in its onCreate method and add the text "Hello World" to the view ". Haha, let's take a look at the Activity with the highest frequency of four Android components.
1. What is Activity? First, I will ask a question: what is an Activity? If the answer to one of the four most frequently used components of Activity is obviously unsatisfactory, What Is Activiyt? Let's take a look at the official explanation:
Activity is an important component for interaction with Android apps. The Activity is responsible for creating a window. Based on this window, the developer can add the UI through setContentView. By default, the full screen display of the Activity can also be used as a floating window display. Activity exists to reduce coupling and separate user-defined data from system calls.
For example, the Activity provides the following processing: control the relationship between the window level and display (for example, the current page has multiple pop-up windows, multiple Toast, and each Dialog and Toat are new windows) in the window, follow the setContentView to pass the View object creation interface. Handles touch events, hard Keyboard Events, and scroll wheel events in windows and views. Draw and respond to all events in the current view. Controls the display and hiding of input methods in the current window. Is Activity a UI? According to my understanding, No. This problem leads to another concept of the UI (User Interface). If we have a rough understanding of the UI, the art works drawn by the artist will be involved, activity is equivalent to a frame. It is mainly used to support the painting, but it cannot be said that the frame is the painting itself.
2. The following articles and pictures can be used to understand the specific life cycle of an Activity and its meaning. However, there are two questions:
Question 1: Why is there a lifecycle? Why are there so many methods available in the lifecycle? The Android Framework layer itself is based on Java. Can't it provide only one entry, such as the mian function? The answer is that Android can provide periodic Callbacks. You can only provide one portal to actually use onCreate as the portal. Why do you need to provide so many callbacks In the lifecycle? In my understanding, the reason is that the performance is improved to reuse existing objects. Android can provide only one interface to create an Activity. If the current Activity is hidden, the current Activty is directly cleared, however, this will cause the user to click the back button to see the previous Acitivyt display page, because it is cleaned up when it is covered, so it is necessary to create a new one, which consumes resources, the main reason is that this process will definitely take time, and during this time, users can only wait to bring poor user experience.
Question 2: Which callback methods will the Dialog display and hide execute the Activity? According to the understanding of the Activity lifecycle, when the Activty interface is partially masked, onPause is executed, and onResume is executed when the masking disappears, however, if you write a Demo and print the log in the corresponding cycle method, the display and hiding of the Dialog will not trigger any lifecycle method. The symptom indicates that the covered trigger Activiyt cycle is incorrectly understood. Although Dialog does not trigger the lifecycle method. However, some articles show that only one Dialog is displayed in the middle of the mobile phone screen, but onPause and onResume are also executed. This is because the covered Dialog interface is actually set the style format of the Activity to theme. Dialog, that is, the Activity occupies only the Middle of the screen in the form of Dialog and does not occupy full screen. The overwriting of a new Activity will certainly trigger onPause and onResume.

It takes two minutes to understand the lifecycle of an Android Activity )! Http://blog.csdn.net/android_tutor/article/details/5772285

Managing the Activity Lifecycle
Http://developer.android.com/training/basics/activity-lifecycle/index.html

Starting an Activity
Http://developer.android.com/training/basics/activity-lifecycle/starting.html

Pausing and Resuming an Activity
Http://developer.android.com/training/basics/activity-lifecycle/pausing.html

Stopping and Restarting an Activity
Http://developer.android.com/training/basics/activity-lifecycle/stopping.html

Recreating an Activity
Http://developer.android.com/training/basics/activity-lifecycle/recreating.html



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.