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

前端 未结 8 1236
星月不相逢
星月不相逢 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:06

    I had a similar issue after adding notifications as found on the android training site. None of the other answers here worked for me, however this answer did work for me. Summary:

    final Intent notificationIntent = new Intent(context, YourActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    

提交回复
热议问题