Resolving android to create a shortcut will start the problem for both applications

  • 2020-05-10 18:48:17
  • OfStack

Let's talk about a perfect solution.
Since the first interface is the welcome interface, the shortcut launch interface is also the welcome interface. At the beginning, I suspected that the code created by the shortcut had a problem, but I didn't know what the problem was. I guess it may be a shortcut error that causes the system to create two applications, which will have two PID for the same application. As a result, I can view the printed LOG in the console. LOG is launched in different ways as follows:


09-22 09:39:11.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: +763ms
09-22 09:39:47.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: +995ms

Finally found two PID is 1 to (Activity InitActivity is welcome screen), may be an accident, compare the two LOG will find from the list, click the icon to start the application is above LOG, through LOG below are shortcuts to start, through the comparison will be found that when they start the application in quick way less than the other way 1 cat = [android. intent. category. LAUNCHER], so in the code plus (please refer to the 1 piece of code, only add the line 3) :

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);  

If you run app 1 and cut OK again, you won't start two apps, and you only need to exit once. Great! As for why, I'll leave it to you.


Related articles: