I have built an app which changes the users wallpaper. I wish to add an Android shortcut so a user can change their wallpaper without having to fully open the app (the main use case is to tie it to a gesture in something like Nova Launcher, which allows you to select a shortcut for each gesture).
I have it all working, with one massive issue. Every time the shortcut is fired, my custom action occurs, but then the main launch activity ALSO launches! This is obviously not desired, and I cannot figure out what is going on.
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 this even possible to do without trigger the main launcher activity?