[Android interview question-3] Four loading modes of activity

Source: Internet
Author: User

Question:Briefly introduce the four loading modes of some activities.

Analysis:The four loading modes areStandard,Singletop,Singletask,SingleinstanceIn androidmanifest. xml. The Android: launchmode attribute of the activity element is set.

1. Standard: execute the following code:

Intent intent = new Intent(); intent.setClass(ActA.this, ActA.class); startActivity(intent); 

A new activity instance is generated in the stack.

2. singletop: the system will detect the execution of the above Code.Stack topThere are already activity instances that meet your needs. Therefore, you can send intent to the instance at the top of the stack without generating a new activity instance. However, if you execute the following code, a new activity instance will be generated becauseStack topThere are no instances that meet your needs.

Intent intent = new Intent(); intent.setClass(ActA.this, ActB.class); startActivity(intent); 

3. singletask: different from singletop, the system checks whether intent is sent every time.Entire stackTo find the desired activity instance. If the instance exists, send the intent to it. If the instance does not exist, a new one is generated.

4. singleinstance: singletask is equivalentIn one applicationA certain type of activity only has one instance, which is shared by everyone. singleinstance is equivalentMultiple applicationsAn instance that shares an activity. For example, a tour guide application calls the map application. You open the map interface in the tour guide application and locate Zhongguancun. Then, press the Home Key to move the tour guide application to the background, then open the Baidu map on your mobile phone and you will find that the Baidu map is set to Zhongguancun. This shows that the tour guide app shares an instance of the same activity with Baidu map.

The following is an introduction in the official document. If you are interested, refer:

Standard: The default mode, which will usually
Create a new instance of the activity when it is started, though this behavior may change with the introduction of other options such
AsIntent.FLAG_ACTIVITY_NEW_TASK.


Singletop: if,
When starting the activity, there is already an instance of the same activity class in the foreground that is interacting with the user, then re-use that instance.
This existing instance will receive a callActivity.onNewIntent()With
The new intent that is being started.


Singletask: if,
When starting the activity, there is already a task running that starts with this activity, then instead of starting a new instance the current task is brought to
Front. The existing instance will receive a callActivity.onNewIntent() With
The new intent that is being started, and withIntent.FLAG_ACTIVITY_BROUGHT_TO_FRONTFlag
Set. This is a superset of the singletop mode, where if there is already an instance of the activity being
Started at the top of the stack, it will receive the intent as described there (without the flag_activity_brought_to_front flag set). See the tasks
And back stack document for more details about tasks.


Singleinstance: only
Allow one instance of this activity to ever be running. This activity gets a unique task with only itself running in it; if it is ever launched again with
Same intent, then that task will be brought forward and itsActivity.onNewIntent()Method
Called. If this activity tries to start a new activity, that new activity will be launched in a separate task. See the tasks
And back stack document for more details about tasks.

Due to the limited level of the author, it is inevitable that there will be errors in the train of thought or code provided for each interview question. Readers should also criticize and correct the question. In addition, I am very grateful to welcome readers to offer more and better interview questions. If you have any comments or suggestions, please refer to the comments.

The blogger Xu fanglei has copyright to this blog article. For Network reprint, please specify the source http://blog.csdn.net/shishengshi. To organize the publications, contact the author.

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.