Android Notification restarts app but want to resume

倾然丶 夕夏残阳落幕 提交于 2019-11-28 21:24:29
ozmike

I put this in manifest

android:launchMode="singleInstance"

This fixed the problem for me. Also this answer which I have not tried which allude to other things of interest.

If you have to avoid setting your activity to "singleInstance", then set the notification's intent as follows:

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

You can try this FLAG_ACTIVITY_REORDER_TO_FRONT (the document describes exactly what you want to)

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT

Intent notificationIntent = new Intent(this, HelloAndroid2.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

I would recommend to set the flag Intent.FLAG_ACTIVITY_SINGLE_TOP for this particular situation, if you use android:launchMode="singleInstance" it would affect the behaviour when you call other intents in your application, example using facebook sdk, paypal sdk etc..

The explanation is simple right here: link

I have another solution working for me here :

    //Resume or restart the app (same as the launcher click)
    val resultIntent = Intent(context, MyLauncherActivity::class.java)
    resultIntent.addCategory(Intent.CATEGORY_LAUNCHER)
    resultIntent.setAction(Intent.ACTION_MAIN)
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    val pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT)
    builder.setContentIntent(pendingIntent)  //builder is the notificationBuilder

Remove this from manifest from .MainActivity activity portion if you have it there:

android:noHistory="true"

This is fatal for not being able to resume app on Samsung devices (and others) with Android newer then 7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!