Activity details of four main components and four major activities

Source: Internet
Author: User

Activity details of four main components and four major activities

1. Summary of the Activity

I have read the source code of the Activity. The comments of the Activity class are probably explained as follows: almost all the activities are used for interaction with users, I want to use a simple display interface to exclude some pure Display Interfaces. Therefore, the Activity class mainly creates a window to display the UI interface we have made, we usually use full screen to display the UI, but the Activity also has some other purposes, for example, it is used as a floating window (change the active Activity in the list file ).

Ii. Four launchmodes of Activity

Settings: <activity android: name = ". MainActivity" android: launchMode = "standard"/>

1,Standard:Setting Activity to Standard Startup Mode means: for example, if the current display interface is FirstActivity, A FirstActivity instance will be displayed in the task stack, and then the FirstActivity will jump to FirstActivity, in this case, a new FirstActivity instance will be generated in the task stack. If you jump to FirstActivity again, the second FirstActivity instance will be added to the task stack, up to now, there have been three FirstActivity instances in the task stack. At this time, we press the Back key twice to return to the first FirstActivity, as shown in:

2,SingleTop:For example, if the current interface displays FirstActivity, there will be a FirstActivity instance in the task stack. Next, the FirstActivity jumps to FirstActivity. At this time, the system finds that there is already a FirstActivity instance at the top of the stack, therefore, the FirstActivity instance will not be created again (such as the first figure). Otherwise, the instance (such as the second figure) will be created at the top of the stack. When the instance jumps from SecondActivity to FirstActivity, it is found that a FirstActivity instance exists in the task stack, but its position is not at the top of the stack. A FirstActivity instance is created for all instances.

3,SingTask:For example, if the current interface is FirstActivity, there will be a FirstActivivity instance in the task stack. Next, the FirstActivity jumps to SecondActivity. The system finds that there is no SecondActivity instance in the task stack, so a new SecongActivity instance will be created and placed on the top of the stack (such as the upper part). Next we will jump from SecondActivity to FirstActivivity, the system will find that the FirstActivivity instance already exists in the stack (no matter where it is located), so it will not create a FirstActivity instance in the stack, at the same time, the stack (such as the lower half) will pop up for all objects on the FirstActivity instance ).

Intent) when the start mode of the Activity is singleTop/singleTask, The onNewIntent method will be executed. For example, if you jump from OneActivity (SingTask mode) to TwoActivity and then return from TwoActivity, the life cycle of the OneActivity will change to onnewint () ----> onResart () ------> onStart () -----> onResume (), that is, the onNewIntent () method will be executed as long as the OneActivity is in the task stack and not the first time it enters the OneActivity. So (2) the onSaveInstanceState (Bundle outState) has checked a lot of information on the Internet, saying that this method will be executed only when the system memory is insufficient and the Activity is forcibly killed, however, this statement is incorrect. I have tried several cases and will also execute the onSaveInstanceState method. 1. The Activity jumps normally. 2. Press the Home Key to return to the background. 3. When switching between landscape and landscape screens, for example, when we watch a movie, we switch between landscape and landscape screens, you must save the progress before switching. 3. onConfigurationChanged (Configuration newConfig) android: onConfigurationChanged actually corresponds to the onConfigurationChanged () method in the Activity. The appeal code added to AndroidManifest. xml indicates that the onCreate () method is no longer executed when the screen direction is changed, the software disk is popped up, and the soft keyboard is hidden,
Instead, you can directly execute onConfigurationChanged (). If you do not declare this code segment, the onCreate () method will be executed according to the Activity lifecycle, while the onCreate () method usually performs initialization before the display. Therefore, if the onCreate () method is executed for all operations such as changing the screen direction, it may cause repeated initialization and reduce program efficiency, in addition, it is more likely that data is lost due to repeated initialization. This must be avoided by ten millions.

Related Article

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.