Starting an activity from a shortcut always launches the main activity

I created an application that changes the wallpaper of users. I want to add a shortcut for Android so that the user can change the wallpaper without having to fully open the application (the main use case is to attach it to a gesture in the form of Nova Launcher, which allows you to select a shortcut for each gesture).

Everything works for me, with one big problem. Each time a shortcut starts, my own action happens, but then the main ALSO launch activity starts! This is clearly undesirable, and I cannot understand what is happening.

Here is the ShortcutActivity code:

public class ShortcutActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final Intent intent = getIntent(); final String action = intent.getAction(); if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) { setupShortcut(); finish(); return; } else { Toast.makeText(this, "Do something interesting.", Toast.LENGTH_SHORT).show(); finish(); } } void setupShortcut() { Intent shortcutIntent = new Intent("CHANGE"); shortcutIntent.setClass(this, getClass()); Intent intent = new Intent(); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_title)); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); setResult(RESULT_OK, intent); } } 

Here is my (simplified) manifest:

 <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/title_activity" android:theme="@style/AppTheme"> <activity android:name=".WallpaperSettings" android:label="@string/title_activity_wallpaper_settings"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ShortcutActivity" android:theme="@android:style/Theme.NoDisplay" android:label="@string/shortcut_title"> <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> 

Is it possible to do without launching the main launch activity?

+5
source share
5 answers

It happened to me. However, I found a working solution. Just add android:launchMode="singleInstance" to the activity that opens from your shortcut.

In this case:

 <activity android:name=".MainActivity2" android:label="@string/title_activity_main_activity2" > <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT"/> <category android:name="android.intent.category.CUSTOM_TEST" /> </intent-filter> </activity> 

I can not find an explanation for this, because I can not find the root of the problem. It just works

Source: fooobar.com/questions/1212248 / ...

+6
source

Try the following changes.

1) setupShortcut method:

 private void setupShortcut() { Intent shortcutIntent = new Intent(getApplicationContext(), ShortcutActivity .class); shortcutIntent.setAction(Intent.ACTION_VIEW); Intent addIntent = new Intent(); addIntent .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_title)); addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher)); addIntent .setAction("com.android.launcher.action.INSTALL_SHORTCUT"); getApplicationContext().sendBroadcast(addIntent); } 

2) declaration in the manifest file:

  <activity android:name=".ShortcutActivity" android:noHistory="true" android:excludeFromRecents="true" android:theme="@android:style/Theme.NoDisplay" android:label="@string/shortcut_title"> <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> 

Hope this will be helpful!

+1
source

So, I tested this with nova launcher and it seems to work fine.

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.geronimo.testapplciation" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity2" android:label="@string/title_activity_main_activity2" > <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT"/> <category android:name="android.intent.category.CUSTOM_TEST" /> </intent-filter> </activity> </application> 

Things I Learned - A category is just text that you can change as you wish.

I can launch MainActivity2 from a shortcut created using Nova Launcher.

If I run the application from the application box, MainActivity1 opens.

Possible problem: if you open the application from the shortcut and minimize the application, the application resumes from step 2.

Point, you might prefer your shortcut engine, just like you. And if this is the same approach / code that you are using. Repeat again.

+1
source

I'm going to go a little “philosophical” here. So, when you create a shortcut, what you want to do is access the application from your home screen, not from the menu. But you want to start another action when you clicked this “shortcut”. This means that you need a start icon for some other activity. As mentioned here , this will not make sense. Instead of what you are looking for, it is a widget that opens another action to suit your needs and can be placed on the screen. For this purpose , the Android developer website has a very detailed explanation.

EDIT . As for your problem, I think that you would add a similar intent filter for both activities, so both start. Because intent filters show that both of these are startup actions.

0
source

Hi, RealRealCasually,

I think you just need to remove the category line from your manifest code.

Just remove: <category android:name="android.intent.category.DEFAULT" /> from your manifest code.

Please check the accepted answer here .

Hope this helps you. Please let me know if this does not solve your problem.

Enjoy the coding ... :)

0
source

Source: https://habr.com/ru/post/1212245/


All Articles