【问题标题】:How to open my application in current state from notification如何从通知中以当前状态打开我的应用程序
【发布时间】:2016-03-29 06:27:23
【问题描述】:

我需要在当前状态下打开应用,而不是我的主要活动的任何新实例

这是我的代码

Intent intent = new Intent(baseActivity, MainActivity.class);
    intent.putExtra("FROM_NOTIFICATION", true);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(baseActivity, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(baseActivity)
            .setSmallIcon(getNotificationIcon()).setLargeIcon(BitmapFactory.decodeResource(baseActivity.getResources(), R.drawable.ic_launcher))
            .setContentTitle(baseActivity.getResources().getString(R.string.app_name))
            .setContentText(message)
            .setOngoing(true)
            .setContentIntent(pendingIntent);
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

我该怎么做?还是有其他方法?

【问题讨论】:

  • “没有我的主要活动的任何新实例”是什么意思?旧实例在哪里?您的意思是,如果您已经在应用程序中,它不应该覆盖“旧”的..?
  • @Vucko 是的。有可能吗?
  • 是的,我现在不在我的笔记本电脑旁边重新检查我正在使用的东西,但我认为它是在 AndroidManifest.xml 中的 @987654325 中添加 android:noHistory="true" @ 你要的那个。如果这不起作用,我将不得不在 4 小时左右与您联系:D
  • 你解决了吗?
  • @RaguSwaminathan no(

标签: android android-intent android-notifications


【解决方案1】:

尝试将标志设置为意图,如下所示..

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

而不是

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);

在您的代码中。

希望这对您有所帮助。 如有疑问请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多