Android implementation does not repeatedly start the APP, androidapp

Source: Internet
Author: User

Android implementation does not repeatedly start the APP, androidapp

Blog reprinted: http://blog.sina.cn/dpool/blog/s/blog_5de73d0b0102vpai.html? Utm_source = bshare & utm_campaign = bshare & utm_medium = weixin & bsh_bid = 883565991

Apps like QQ and so on generally do not open two instances.
For example, open the QQ client, go to the friend chat page, and press HOME to return to the desktop.
There are two situations at this time:
1. If you switch to the QQ client in the latest task, the chat page is displayed.
2. If you click the QQ client icon to open the client, the chat page is still displayed.
By default, the above functions cannot be implemented.
However, android: alwaysRetainTaskState = 'true' can be used to meet our needs.
What we need to do is:
In the Androidmanifest. xml file, add android: alwaysRetainTaskState = 'true' to the root Activity.
The root Activity is the Activity started by the application by default. Generally, you are welcome to the page SplashActivity or the MainActivity on the main page.
As shown in:

  

A brief explanation:
Android: alwaysRetainTaskState, as its name implies, is always in the task stack state]
A Task refers to a Task stack, which is used to record the opening sequence and saving status of the Activity.
For example, the order to open the client is SplashActivity --> GuideActivity --> MainActivity
(Welcome page --> feature boot page --> Home Page)
In the job stack,

  

Because we are in Androidmanifest. in the xml file, set android: alwaysRetainTaskState = 'true' for SplashActivity. When we press the HOME Key to return to the desktop, the status of the task stack is retained, when we click the application icon to open the application again, the system will determine whether a stack with SplashActivity as the root Activity already exists. If so, the stack will be used directly and the Activity at the top of the stack will be displayed. Note: you only need to set the root Activity.

Recently I found a method,
It is Activity. moveTaskToBack (boolean). This method can run the entire task stack in the background, which is equivalent to minimizing the number of PC windows! Very powerful! Generally, in order to ensure that the application runs in the background and increase the speed of re-opening, the application will not be directly finished () (For details, refer to long-staying applications such as QQ and Weibo) to implement this function, you can call the following code when you press the return key:

@ Overridepublic void onBackPressed () {// press the return key to return the desktop Intent intent = new Intent (Intent. ACTION_MAIN); intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); intent. addCategory (Intent. CATEGORY_HOME); startActivity (intent );}

Although this method can implement the functions we need, if a user installs multiple desktop applications, a dialog box may be displayed asking the user to select a desktop to run. However, if you change the code to the following:

@ Overridepublic void onBackPressed () {// press the return key to return the desktop moveTaskToBack (true );}

The above problem will not occur !! Others, QQ is also implemented in this way. There is a slight difference. The former returns the default desktop page, and the latter returns the last desktop page.

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.