Reputation: 3613
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?
Upvotes: 7
Views: 3988
Reputation: 4256
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
Upvotes: 8
Reputation: 3504
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.
Upvotes: 1
Reputation: 23596
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... :)
Upvotes: 0
Reputation: 15358
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 !!
Upvotes: 1
Reputation: 3504
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.
Upvotes: 0