Resolve the problem that two applications will be started when creating shortcuts for android

Source: Internet
Author: User

Next we will talk about a perfect solution.
Because the first interface is a welcome interface, the shortcut startup interface is also a welcome interface. At the beginning, I suspect that there is a problem with the Code created by the shortcut, but I don't know where the problem is. A shortcut error may cause the system to create two applications. The same application has two PIDs, and the printed LOG is displayed on the console,Logs started in different ways are as follows:

Copy codeThe Code is as follows: 09:39:11, 09-22. 929: INFO/ActivityManager (61): Starting: Intent {act = android. intent. action. MAIN cat = [android. intent. category. LAUNCHER] flg = 0x10200000 cmp = com. android. xxx /. activity. initActivity} from pid 19304.
09-22 09:39:12. 876: INFO/ActivityManager (61): Displayed com. android. xxx/. activity. InitActivity: + 763 ms

09:39:47 09-22. 668: INFO/ActivityManager (61): Starting: Intent {act = android. intent. action. MAIN flg = 0x10000000 cmp = com. android. xxx /. activity. initActivity bnds = [5,392] [115,510]} from pid 19304
09-22 09:39:48. 737: INFO/ActivityManager (61): Displayed com. android. xxx/. activity. InitActivity: + 995 ms

At last, we found that the two PIDs are consistent (InitActivity is the Activity on the welcome page), which may be an accident. When we compare the two logs, we will find that the above LOG is used when we click the icon from the list to start the application, the following LOG is started through the shortcut method. After comparison, we can find that the cat = [android. intent. category. LAUNCHER], so you can add it to the Code (refer to the previous Code and add only the third line ):Copy codeThe Code is as follows: ComponentName comp = new ComponentName (this. getPackageName (), this. getPackageName () + "." + this. getLocalClassName ());
Intent intent = new Intent (Intent. ACTION_MAIN). setComponent (comp );
Intent. addCategory (Intent. CATEGORY_LAUNCHER );
Shortcut. putExtra (Intent. EXTRA_SHORTCUT_INTENT, intent );

Running the application again is okay. It won't start two applications, and it only takes one time to exit. This is awesome! As for why we leave it to the visitor.

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.