我正在尝试为我的 Android 应用程序创建一个快捷方式,在打开时调用一些代码。我已经按照官方指南创建了快捷方式,它在我的智能手机上正确显示: 。
但是,我在
onResume
的 MainActivity
函数中使用一些代码,以便在使用快捷方式时调用一些代码。问题是通过快捷方式打开应用程序时不会调用 onResume
。当我通过按图标正常打开应用程序时,会调用 onResume
函数。
我必须进行任何配置选项才能使快捷方式“恢复”我的主要活动吗?
AndroidManifest.xml
的重要部分:
<!-- Between the application tag -->
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>
res/xml/shortcuts.xml:
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="play_favourites"
android:enabled="true"
android:icon="@drawable/heart_outline"
android:shortcutShortLabel="@string/play_favourites_short_label">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="ch.silvanschmidt.fimusic"
android:targetClass="ch.silvanschmidt.fimusic.MainActivity" />
</shortcut>
</shortcuts>
我希望当我使用快捷方式打开应用程序时运行
onResume
函数。但是,只有当我使用普通图标启动器时它才会恢复。
您可以在启动器活动中尝试一下吗:
android:noHistory="false"