7

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?

RealCasually
  • 3,593
  • 3
  • 27
  • 34

5 Answers5

8

It happened to me also. However I found a working solution. Just add android:launchMode="singleInstance" to the activity getting opened 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 cannot find an explanation to this, because cant really find the problem root. It just works

Source: https://stackoverflow.com/a/23002294/1354096

Community
  • 1
  • 1
BamsBamx
  • 4,139
  • 4
  • 38
  • 63
  • This seems to be doing exactly what I needed. The shortcut activity no longer opens the main activity, even if it was minimized before. Thank you. – Waboodoo May 28 '16 at 09:07
1

Give it a try by doing 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 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>

I hope it will be helpful !!

Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
  • I'm afraid that didn't help. First, by not calling setResult, the shortcut won't show up at all. Once I do that, the shortcut shows up but the same issue of the main activity showing happens again. Thanks anyways. – RealCasually Feb 06 '15 at 07:24
1

So i've tested this with nova launcher and it seems to be working 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 which i learnt -the category is merely text which you can change as per your will

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

If I launch the application from the apps drawer,MainActivity1 opens.

Possible Issue:if you open the app from the shortcut,and minimize the application,the application resumes from activity 2.

Point being,you could prefer your shortcut making mechanism like you are.And if this is the same approach/ code that you are using.Well am stumped,again.

Droidekas
  • 3,464
  • 2
  • 26
  • 40
0

Am going to go a bit "philosophical" here.So when you create a shortcut,what you want to do is reach the application from your home screen instead of the menu. But what you want is to launch another activity when that "shortcut" is clicked.That means you want a launcher icon for some other activity.As mentioned here,this would not make much sense. Instead what you are looking for,is a widget which opens another activity as per your needs and can be placed on the screen.For that purpose,the Android developer website has a very detailed explanation.

EDIT:as for your issue,I think you would have added a similar intent-filter for both activities hence both get launched.Because the intent-filters indicate that both of them are launcher activities.

Community
  • 1
  • 1
Droidekas
  • 3,464
  • 2
  • 26
  • 40
  • I don't think thats quite right. I want an Android shortcut (the Android concept called Shortcut, not just the word meant generically) specifically so users of apps like Nova Launcher and Tasker can utilize it to perform a function without seeing any UI at all. – RealCasually Feb 04 '15 at 17:47
  • But how would the user obtain said shortcut?Is it a drag and drop from the applications menu? – Droidekas Feb 04 '15 at 17:50
  • Sorry,just figured out your use case.My bad. – Droidekas Feb 04 '15 at 17:52
0

Hello RealRealCasually,

i guess you just need to remove category line from your manifest code.

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

Please check accepted answer here.

Hope it will help you. Please let me know if it not resolving your issue.

Enjoy Coding... :)

Community
  • 1
  • 1
Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188