Rishabh876 Rishabh876 - 3 years ago 40
Android Question

App Shortcut only works once, just resumes the app after that

I have a dynamic app shortcut which looks like this

ShortcutInfo composeShortcut = new ShortcutInfo.Builder(App.getInstance(),
.setIcon(Icon.createWithResource(App.getInstance(), R.drawable.compose_icon))
.setIntent(new Intent(context, ComposeActivity.class))

First time when I click this shortcut, it opens
just fine but not after that, I have to kill the app after which it works again for one time.

Desired flow - Whenever I click the app shortcut, the ComposeActivity should open. If the app is already running then ComposeActivity should open on top of the current activity only if the current activity is not already a ComposeActivity.

Current flow - First time I press the app shortcut, Compose activity opens just fine, but not after that. App is only resumed after that. I have to kill the app and again click the shortcut for it to open the ComposeActivity.

More info -
Compose Activity in AndroidManifest is defined like this:


<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>

Answer Source

So I finally found the solution. Thanks to some useful information from David Wasser.

Firstly App shortcut works for Activity with Action MAIN and Category LAUNCHER.

So in manifest I added the following intent filter to my ComposeActivity

 <Activity android:name=".view.activity.ComposeActivity"
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>

If you look closely I have also set android:relinquishTaskIdentity="true". This makes sure that if I have already open the ComposeActivity from shortcut and then I can again use it and open ComposeActivity.

Next I updated the Intent used for Shortcut

new Intent(context, ComposeActivity.class))

By doing this, ComposeActivity will always open if it already not on top of the activity stack. Also it will open on top of any existing activity.

But there is one problem. If there is no activity open, then Compose will not have any activity below it. App will close if back button is pressed.

One thing you can do is

public void onBackPressed() {
        if (getIntent().hasCategory(Intent.CATEGORY_LAUNCHER) 
              && Intent.ACTION_MAIN.equals(getIntent().getAction())) {
            //launch any activity

With this I could have any activity to open when ComposeActivity is the only activity in the back stack. In my case I launched my HomeActivity.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download