android - 恢复(前台启动) Activity 而不更新 ImageIcons

标签 android android-intent android-activity android-service android-notifications

我有一个音乐播放器服务,当它进入前台时我会向用户显示通知。但是,当用户单击通知返回应用程序时,它会再次更新图标(图标图像在 onCreate 中设置)。我知道它正在更新图标,因为它正在重新创建 Activity 。

这是一段代码:

    public void startForeground(String songName){

        Intent showTaskIntent = new Intent(AudioPlayer.this, PlayerActivity.class);
        showTaskIntent.setAction(Intent.ACTION_MAIN);
        showTaskIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        showTaskIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent contentIntent = PendingIntent.getActivity(
                this,
                0,
                showTaskIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

Notification notification = new Notification.Builder(getApplicationContext())
                .setContentTitle(getString(R.string.app_name))
                .setContentText(songName)
                .setSmallIcon(R.drawable.logo)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(contentIntent)
                .build();

        startForeground(NOTIFICATION_ID, notification);

我尝试了许多不同的标志,但它总是会更新。堆栈中的其他相关案例和解决方案对我没有帮助。

--

主要问题 - 解释:

  1. 例如,当您打开音乐播放器时,您会看到播放按钮。
  2. 您点击播放,它就会播放。播放图片改为暂停图片。
  3. Activity 进入前台。还在播放。
  4. 您点击通知。 Activity 回来(onCreate、onStart、onResume)。然后它将暂停图像更改为播放图像。

    • 如果我单击应用程序图标(系统图标而不是通知),它会调用 onRestart。 我想这就是我所需要的。

希望有人能帮助我。

最佳答案

android:launchMode="singleTask"添加到AndroidManifest.xml中的主要 Activity

关于android - 恢复(前台启动) Activity 而不更新 ImageIcons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33908788/

相关文章:

java - 如何从返回 double 组的方法中获取所有 double 值?

android - 旋转或缩放后的 View 中心

android - 如何让我的 Activity 的单个实例与后台堆栈一起工作?

android - Main Activity 总是第一个启动的 Activity 吗?

java - setOnEditorActionListener 给出 NullPointerException

android - 在添加Fabric的崩溃报告后,我无法在gradle中添加新的库

android - 如何在 Kotlin 中关闭 Android 应用程序

android - 如何从 Intent 调用创建自定义 PopupWindow?

java - 如何在另一个 Activity 中使用字符串数组

java - 当 Activity 重新启动时, Activity 的 fragment 如何恢复?