preface

Google has a New Shortcuts Api for “Shortcuts” and this article will look at Shortcuts from a Map app

Reference effect

A map shortcut effect display

Extracting function points

  1. Hold down to display a list of shortcuts

  2. Display AD page

  3. Jump to target page

  4. Return to the target page

The specific implementation

Create shortcuts

Static and dynamic creation are officially provided. Dynamic creation is more flexible and is mainly introduced here.

The code is very simple, just look at the comments

Val shortcutItemList = mutableListOf<ShortcutInfoCompat>() // used to demonstrate just creating a "go from here" shortcut //1. Build task stack list val intentList = arrayOf() //2 Create shortcut entry val shortcutItem = ShortcutInfoCompat // Set unique Id.Builder(context, SetShortLabel (" start from here ").setlongLabel (" start from here ") // Set icon .setIcon(IconCompat.createWithResource(context, SetIntents (intentList).build() shortcutitemlist.add (shortcutItem) //3. The custom shortcuts list import manager ShortcutManagerCompat. SetDynamicShortcuts (context, shortcutItemList)Copy the code

Set the Intent

The Intent refers to the interface that we want to jump on after clicking on the shortcut. Normally, setIntent() is ok, but after jumping to the target interface, we click on return and need to return to the home page. Then we can use the setIntents() function.

Website shows

Using ShortcutInfoCompat. Builder to create a shortcut, you can use setIntents () instead of setIntent (). By calling setIntents(), you can start multiple activities within the application when the user selects a shortcut and put all the activities in the list on the return stack except the last one…

Target interface analysis

The core functionality we need to implement

  • Long press shortcut (Ads -> Navigation)
  • Click Back (Navigation page -> Home page)

Here, I take the home page as the first interface and the AD page as the “target page”. When the AD page starts, I immediately jump to the navigation page and finish the AD page. At this time, there are only home page and navigation page in the task stack. Also achieved the target effect

Complete intentList

IntentList = arrayOf(intentList = arrayOf(intentList = arrayOf(intentList = arrayOf(intentList = arrayOf)) MainActivity::class.java).apply { action = Intent.ACTION_MAIN flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK}, // SplashActivity is the page that handles the Shortcuts logic Intent(context, SplashActivity: : class. Java). Apply {/ / TODO targets shortcut must set up the action, or an error, can be arbitrary action action = Intent. ACTION_VIEW / / TODO Flags = Intent.FLAG_ACTIVITY_NEW_TASK //TODO putExtra("action_id", "guide")}Copy the code

Manifest

 <activity
            android:name=".SplashActivity"
            android:excludeFromRecents="true"
            android:noHistory="true"
            android:taskAffinity="">
        
        </activity>
Copy the code

Pay attention to the point

  • When creating an Intent, you must set an action

  • TaskAffinity is best configured for target interface (official recommendation)

  • Using taskAffinity best specified excludeFromRecents noHistory, or task stack there will be a record

Static shortcuts for starting an Activity from another Activity cannot have custom intent flags. The first intent in a static shortcut is always set to intent.flag_activity_new_task and intent.flag_activity_clear_task. This means that if the application is already running, all existing activities in the application will be destroyed when the static shortcut starts. If you don’t want this behavior, you can use Trampoline Activity, Or use an invisible Activity that starts other activities in activity.oncreate (Bundle) and then calls activity.Finish () :

  1. In the Androidmanifest.xml file, the Trampoline Activity should include the attribute assigned android:taskAffinity=””.
  2. In the shortcut resource file, an intent within a static shortcut should reference a Trampoline Activity.

Implement jump logic

When you build an Intent, you can pass in custom parameters and control the jump logic. Demo code is very simple

class SplashActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {super.oncreate (savedInstanceState) setContentView(r.layout.activity_splash) //TODO differentiates between default startup and normal startup val action = intent.getStringExtra("action_id") handlerAction(action) } fun handlerAction(action: String?) { when (action) { "guide" -> startToGuide() else -> startToMain() } } fun startToMain() { thread { sleep(2000) val intent = Intent(this, MainActivity::class.java) startActivity(intent) finish() } } fun startToGuide() { thread { sleep(500) val intent = Intent(this, GuideActivity::class.java) startActivity(intent) finish() } } }Copy the code

Results show

Shortcuts will make it easy to add Shortcuts to the entry features of your App with Shortcuts

Although the current customization capacity is limited, but basically meet the development needs, is really Biu times cool

Pay special attention to

  • Shortcuts display differently, and ROM implementations vary from vendor to vendor

  • The ability to customize ICONS is limited

  • It is best not to modify the font (after switching the light/dark mode, the text color will be automatically modified, manual modification will be invalid)

  • Dynamic state shortcut, only after the App has been started at least once, it is not required for subsequent startup (long press if the App has not been started for the first installation)