Android Question [SOLVED] Shortcut example not working

JohnC

Well-Known Member
Licensed User
I just tried the shortcut example (https://www.b4x.com/android/forum/threads/add-shortcuts-to-your-android-application.11444/) and I am getting an error.

When I try to place a new shortcut on the homescreen, I get a "App Isn't installed" error (reported by the launcher) on two different phones (Samsung S4 and a Nexus) running two different OS (Android 5.0.1 and 7.1.1).

Any ideas what is wrong?

(I did have to add the below code into the manifest because the example didn't include it for some reason)

B4X:
AddApplicationText(<activity android:windowSoftInputMode="stateHidden" android:launchMode="singleTop" android:name="shortcutactivity"
            android:label="Add Shortcut" android:screenOrientation="unspecified">
         <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
      </activity>"
)
 
Last edited:

DonManfred

Expert
Licensed User
Last edited:

JohnC

Well-Known Member
Licensed User
The old shortcuts method no longer works with newer versions of Android. You should use the new method: https://www.b4x.com/android/forum/threads/support-for-app-shortcuts.86663/#content
What version of android does this "old" method stop working at?

I need the ability to prompt the user for different options *when they are placing* the new icon on the homescreen. There could be hundreds of different possible combinations and I don't want to limit the user to only a few different icons, so being limited to a fixed amount of icons in the manifest using this "new" method is not really practical. With the "old" shortcut method, I could modify the Activity_Create routine to prompt the user to select options for the icon right before it is placed on the homescreen and this would allow them to create as many icons as desired, each with different options.

Don, thank you for your suggestion, but I need it to work on Android down to 4.4/5.0.

I did find this method that can create shortcuts dynamically from within my app and it works on 5.0/7.1:
https://www.b4x.com/android/forum/t...-directly-from-within-your-application.17559/

But I really wanted to offer the user the convenience of being able to add a shortcut to their launcher via the "Add Shortcut" method (and not from within my app which will randomly position the new icon on their launcher)

There is an app in the playstore: https://play.google.com/store/apps/details?id=com.caramellabs.emailme that prompts the user to select options when *placing* the icon on the homescreen and this functionality works on both 5.0 and 7.1.1.

Any ideas how this app does this?
 
Last edited:

JohnC

Well-Known Member
Licensed User
I came up with the idea to use the "Old" method to have my app listed in the available widgets/shortcuts and allow the user to at least select it, then instead of running the old way of creating the icon, I was going to replace that code with the code here: https://www.b4x.com/android/forum/t...-directly-from-within-your-application.17559/. This way I was hoping to get the best of both worlds - be on the homescreen shortcut list and be able to create an icon in old and new android versions.

So, it looks like the "old" manifest code will properly register the app as a "Widget/Shortcut" allowing the user to select it from a pop-up list of icons they can add to their homescreen.

And the manifest code includes the activity to be "run" (shortcutactivity) when the user selects my app from the pop-up shortcut list via this entry:

B4X:
AddApplicationText(<activity android:windowSoftInputMode="stateHidden" android:launchMode="singleTop" android:name="shortcutactivity"
I then adding this code to the activity that should run when the user selects my shortcut:

B4X:
Sub Activity_Create(FirstTime As Boolean)
  
    Log("Shortcutactivity")
But the log function never runs, so that's probably why I'm getting the "App isn't installed" error because it is not even able to run this activity at all - way before it even tries to make the icon.

Any ideas why execution doesn't jump to the specified activity even on android 5.0?
 

JohnC

Well-Known Member
Licensed User
OK, I got the "Old" method working on BOTH old (5.0.1) and new (7.1.1) android versions!

Basically, it seems that you need to specify the "main" activity in the manifest code (because it can't launch the "shortcutactivity" activity)

So you need to make two changes to the original sample code - I was unable to post these edits in the original thread for this sample because it is locked (https://www.b4x.com/android/forum/threads/add-shortcuts-to-your-android-application.11444/):

Modify the manifest and replace "shortcutactivity" with "main":

B4X:
AddApplicationText(<activity android:windowSoftInputMode="stateHidden" android:launchMode="singleTop" android:name="main"
            android:label="Add Shortcut" android:screenOrientation="unspecified">
         <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
      </activity>"
)
Then modify the Main Activity_Resume code to this:

B4X:
Sub Activity_Resume
    Dim in As Intent
    in = Activity.GetStartingIntent
    Log(in)
    If in.HasExtra("from_shortcut") And in.GetExtra("from_shortcut") = True Then
        Msgbox("Launched from shortcut", "")
    
    Else if in.Action="android.intent.action.CREATE_SHORTCUT" Then
    
        Dim shortcutIntent As Intent
        shortcutIntent.Initialize("", "")
        shortcutIntent.SetComponent("anywheresoftware.b4a.samples.test/.main")
        shortcutIntent.PutExtra("from_shortcut", True)
        Dim in As Intent
        in.Initialize("", "")
        in.PutExtra("android.intent.extra.shortcut.INTENT", shortcutIntent)
        in.PutExtra("android.intent.extra.shortcut.NAME", "Shortcut Test")
        in.PutExtra("android.intent.extra.shortcut.ICON", LoadBitmap(File.DirAssets, "small_logo.png"))
 
        Activity.SetActivityResult(-1, in)
        Activity.Finish
    End If
End Sub
You can also delete the ShortcutActivity because it is no longer needed.
 
Last edited:

JohnC

Well-Known Member
Licensed User
I just made the fix to the original code even easier, and now it will properly launch the ShortcutActivity in the sample project!

Simply add this to the Manifest (no changes needed to the main activity in the sample):

B4X:
AddApplicationText(<activity android:windowSoftInputMode="stateHidden" android:launchMode="singleTop" android:name="shortcutactivity"
            android:label="Add Shortcut" android:screenOrientation="unspecified">
      </activity>"
)

AddActivityText(ShortcutActivity,
        <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>)
A user (nerdworld) added the second section (AddActivityText), but I had to change the first phrase from "Shortcut" to "ShortcutActivity" and then this entry allows the "ShortcutActivity" to be launched properly.

I tested this on 5.0.1 and 7.1.1.

UPDATE 4/27/19: I found out the Intent-filter declaration is not needed in the first "AddApplicationText" and is just needed in the second "AddActivityText" only. So, I removed it.

NOTE: What is cool about this method is that when the user goes to place the icon on their homepage, you can add UI code to the ShortcutActivity that will prompt the user to specify options for the icon before it is even created!

For example lets say the user wants to create an icon that jumps right to your apps settings activity. When the user selects this special option, you can then can stuff special configuration data into the icon like this:

B4X:
shortcutIntent.PutExtra("StartSettings", True)
Then when your app launches, you can check in the Main activity's Activity_Resume sub to see what options the icon was configured for and do those special actions only when your app is launched by that particular shortcut:
B4X:
If in.HasExtra("StartSettings") And in.GetExtra("StartSettings") = True Then
       StartActivity(Settings)
End if
 
Last edited:
Top