代码之家  ›  专栏  ›  技术社区  ›  Ali

从通知项恢复被终止的应用程序后,存在两个应用程序实例

  •  1
  • Ali  · 技术社区  · 4 年前

    我在Github中有一个项目,我在其中使用了导航组件: https://github.com/alirezaeiii/Compose-Sample

    在详细信息屏幕中,我有一个deepLink按钮,可以创建一个待定的意图和一个通知:

    val pendingIntent = findNavController()
                        .createDeepLink()
                        .setDestination(R.id.detailFragment)
                        .setArguments(args.toBundle())
                        .createPendingIntent()
                    Notifier.postNotification(args.poster, requireContext(), pendingIntent)
    

    以下是我生成通知的方式:

    fun postNotification(poster: Poster, context: Context, intent: PendingIntent) {
            val summary = NotificationCompat.Builder(context, channelId)
                .setContentTitle(poster.name)
                .setContentText(poster.release)
                .setAutoCancel(true)
                .setContentIntent(intent)
                .setSmallIcon(R.drawable.donut_with_sprinkles)
                .setGroup(poster.id.toString())
                .setGroupSummary(true)
                .build()
    
            val notification = NotificationCompat.Builder(context, channelId)
                .setContentTitle(poster.name)
                .setContentText(poster.release)
                .setAutoCancel(true)
                .setContentIntent(intent)
                .setSmallIcon(R.drawable.donut_with_sprinkles)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setGroup(poster.id.toString())
                .build()
    
            val notificationManager = NotificationManagerCompat.from(context)
            notificationManager.apply {
                notify(
                    "summary ${poster.id}",
                    0,
                    summary
                )
                notify(
                    "notification ${UUID.randomUUID()}",
                    0,
                    notification
                )
            }
        }
    

    当我生成多个通知,然后关闭应用程序,从通知项恢复详细信息屏幕并退出应用程序时,我从启动器图标启动应用程序,似乎存在两个应用程序实例,因为我需要多次单击“上一步”按钮退出应用程序。我该怎么解决呢?

    附录

    我找到的一个可行的解决方案是将singleTop作为启动模式添加到Activity中,而不是默认的标准模式:

    android:launchMode="singleTop"
    
    0 回复  |  直到 4 年前