Android implementation does not restart the app method

Source: Internet
Author: User

Similar to QQ, such an app, generally will not open two instances.
For example: Open the QQ client, go to the friend chat interface, and then press home to return to the desktop
There are two situations:
1, if you in the recent task to switch QQ client, then still show Friends chat interface
2, if you click on the QQ client icon opens, still show Friends chat interface
It is not possible to implement the above functionality by default.
However, using android:alwaysretaintaskstate= ' true ' can achieve our needs.
What we are going to do is:
In the Androidmanifest.xml file, add android:alwaysretaintaskstate= ' true ' to the root activity, and you can.
Root activity is the activity that the app starts by default, usually the Welcome page splashactivity or the main page mainactivity.
As shown in the following:

Simply explain:
Android:alwaysretaintaskstate as the name implies "Always keep the task stack state"
Tasks refer to the task stack, which is used to record activity opening order, save state, and so on.
For example, the order in which clients are opened is splashactivity---guideactivity-mainactivity
(Welcome page---Function Guide page--Homepage)
Well, it's in the task stack,

Since we set android:alwaysretaintaskstate= ' true ' to splashactivity in the Androidmanifest.xml file, when we press the home key to return to the desktop, the status of the task stack is preserved, When we click the app icon to open the app again, the system will determine if there is already a stack with splashactivity as its root activity, and if so, use the stack directly and show the activity at the top of the stack. Note that you only need to set the root activity.

A method has been discovered recently,
It is Activity.movetasktoback (Boolean), this method can put the entire task stack to run in the background, the equivalent of the PC window to minimize the same! Very powerful! In general, in order to ensure that the application in the background to run, improve the speed of opening again, generally will not directly finish (), (you can refer to QQ, Weibo, these long-stay applications) in order to achieve this function, the more common practice is that when the user presses the return key, call the following code implementation:

@Overridepublic void onbackpressed () {//press the return key to return to 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 there are many desktop applications installed by users, a dialog may be asked to select a desktop to run. However, if you change the code to this:

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

There will be no such problem!! Other, QQ is also realized in this way. There is a slight difference. The former returns to the desktop's default page, which returns to the page where the desktop last stayed.

Android implementation does not restart the app method

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.