Intent - if activity is running, bring it to front, else start a new one (from notification)

前端 未结 8 1237
星月不相逢
星月不相逢 2020-11-27 13:57

My app has notifications, which - obviously - without any flags, start a new activity every time so I get multiple same activities running on top of each other, which is jus

8条回答
  •  孤城傲影
    2020-11-27 14:25

    I know it is old, but nothing from the above were fitting to my application.

    Without changing manifests and other configuration, here is the code to bring your app back to front - or opening it when it is closed

    Intent notificationIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
    notificationIntent.setPackage(null); // The golden row !!!
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    

提交回复
热议问题