Everything you need to know about Activity

Source: Internet
Author: User

Everything you need to know about Activity
Recently, I want to write a blog about the source code analysis of the Activity startup process. Before that, I would like to summarize some basic knowledge required by the Activity in Android to help you understand the source code of the Activity later. 1. The following figure shows the most typical startup modes of an Activity, including onCreate -- onStart -- onResume -- onPause -- onStop -- onDestory. in these stages, this is an android developer who must be familiar with it. I will not describe it in detail here. I just want to explain some precautions: 1. the difference between onStart and onStop: onStart indicates that the application is visible, but only runs in the background, not to the foreground. OnStop indicates that acitvity runs in the background. When a user uses a transparent topic, the difference between onstop.2.onResume and onpause is not called: the two are called back and forth from whether the activity is on the foreground, and they are a group. 3. Before a new Activity is started, the top-level acitivity of the stack must be started with onPause. Therefore, time-consuming operations cannot be performed in onpause. 4. When the activity stops abnormally, onSaveInstanceState is called and the stored Bundle is passed to onRestoreInstanceState. onRestoreInstanceState is called after onStart. The process is that the onSaveInstanceState first saves the data, the Activity will delegate the Window to save the data, and then the Window will delegate the top-level container to save the data, until the DecorView, finally, it notifies its sub-elements one by one to store data. Typical delegated thinking. If onRestoreInstanceState is called, its onSaveInstanceState parameter must have a value. 5. If the activity is killed due to insufficient resources, onSaveInstanceState is also called. Activity has three priorities: * foreground activity. * Visible but not foreground activity (such as Dialog) * background activity (onstop ). Ii. Start Mode 1. Start mode Introduction: android has four modes: standard, singleTop, singleTask, and singleInstance. Standard: A Job stack can have multiple instances, each of which can belong to different job stacks. Each time you create an activity, a new instance is created. When an instance is created, it exists in the stack where the instance is located. If A starts B, B is located in the stack of. You cannot start appliactionContext because the non-activity context does not have a task stack. The solution is to add the FLAG_ACTIVITY_NEW_TASK identifier to create a new task stack. SingleTop: If an activity has a stack top, the activity will not be rebuilt, and its oncreate and other methods will not be re-called. If there is ABC in the stack and C is started again, C will not be re-created. SingleTask: in the stack reuse mode, first judge whether the required stack exists or not. If so, as long as the stack exists, no instance is created and cleanTop is created. If not, create an instance. If none of the stack is available, create a new stack. If the stack contains ABCD, C is started, C is specified, and the stack does not exist, a new stack is created and C is saved. At this time, two stacks exist, one is ABC and the other is C. If the stack contains ABCD, the stack required to start C is the stack where ABCD is located, then C will be placed in the stack item, and all instances on C will go out of the stack, now the stack is changed to ABC. How to specify the required task stack. Generally, TaskAffinity is used to specify the parameter. SingleInstance: all features of singleTask. The difference is that the acitivity with this mode can only be located in a single stack. For example, if A is created and A is in A stack, no instance will be created after A is created. There are two ways to set the Startup Mode: Set andorid: launchMode = "singleTop" in xml: Set intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); the advantage of this method is that the priority is higher than the first one. For example, if two methods are used at the same time, the second method is used. The disadvantage is that it cannot set singleInstance. 2. required job Stack: TaskAffinity: identifies the job stack name required by an activity. The default value is the package name of the application. It is mainly used together with singleTask. Task stacks are divided into front-end and back-end task stacks. The background Task Stack is in the paused state. You can switch the background to the foreground again. If A starts B, B will be located in A's job stack. (Without TaskAffinity) for example, if A starts B and B's TaskAffinity is different from that of A, A new job stack is created and B is placed. AB is the front-end stack, CD is the back-end stack, B starts D, it turns into ABCD, and C is turned into ABC.3. flag: We often specify the flag in the code, FLAG_ACTIVITY_NEW_TASK: similar to rule: similar to singleTopFLAG_ACTIVITY_CLEAN_TOP: Clear the sequence above the current activity Stack: activity with this mark will not appear in the History activity list. You only need to set android: excludeFromRecents = "true". 3. There are two types of IntentFilter matching rules to start the activity: explicit and implicit. If they are implicit, The IntentFilter matching rules must be met. Intent has three attributes: action, category, and data. Only one intent matches the action at the same time. category and data are exactly matched. Multiple filters can be matched. Only one action can be matched. Category: If intent has this attribute, all its category must match. No. There is no DEFAULT property. To accept implicit calls for our activity, the property android. intent. category. DEFAULT must be added. Data: similar to action. It is divided into two parts. MimeType, URL. mineType refers to the media type, such as image/jpeg, URL http://www.baidu.com: 80/search/infoURL is content and file by default, it is matched without specifying data. The call method is intent. setDataAndType (Uri. parse ("file: // abc"), "image/png") the complete code is as follows: Intent intent = new Intent ("com. lxj. a "); intent. addCategory ("com. lxj. B "); intent. setDataAndType (Uri. parse ("file: // abc"), "image/png") startActivity (intent); match the following activity: Let's write so much. Next I will introduce my understanding of the activity startup source code.

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.