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?
If both application have the same signature (meaning that both APPS are yours and signed with the same key), you can call your other app activity as follows: Intent LaunchIntent = getActivity(). getPackageManager(). getLaunchIntentForPackage(CALC_PACKAGE_NAME); startActivity(LaunchIntent);
If the user presses or gestures Back, the current activity is popped from the stack and destroyed. The previous activity in the stack is resumed. When an activity is destroyed, the system does not retain the activity's state.
FLAG_ACTIVITY_NEW_TASK is equivalent to launchMode=singleTask and in there I read. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance.
This is the default launch mode of an activity (If not specified). It creates a new instance of an activity in the task from which it was started. Multiple instances of the activity can be created and multiple instances can be added to the same or different tasks.
Suppose you have A, B and C activities and your activity D has “launch mode = singleTask”. Now you launching activity D - Suppose you have A, B, C and D activities and your activity B has “launch mode = singleTask”. Now you again launching activity B-
Suppose you have A, B and C activities and your activity D has “launch mode = singleTop”. Now you launching activity D - State of Activity Stack after launch D activity Suppose you have A, B, C and D activities and your activity D has “launch mode = singleTop”. Now you again launching activity D -
If you run the app and tap the button on the first activity, the second activity starts but is empty. This is because the second activity uses the empty layout provided by the template. Figure 1.
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
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 !!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With