Reputation: 321
I have an Android application that can perform certain actions. However, I also allow the creation of shortcuts on the homescreen to perform certain things. There is a certain thing you can do in the app by pressing a button in it which calls a method (lets call the method DoAction), that I want to allow users to do directly from a shortcut. When clicking the shortcut, it opens the main activity and calls DoAction just as the button does, and then calls finish() on the activity to close it.
However, a problem arises when the app is already open in RAM (minimized). After calling finish() in the activity created by the shortcut, the old running instance of the app is brought to the front (which I don't want to happen).
How can I get around this?
Upvotes: 0
Views: 1758
Reputation: 321
This is the working code I used to create the shortcut:
Intent shortcutIntent = new Intent(ShortcutActivity.this, com.example.myproject.ClassToOpen.class);
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); // This was the line that I needed to add
ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(ShortcutActivity.this, R.drawable.shortcut);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name");
setResult(RESULT_OK, intent);
Upvotes: 2