Notification bring app to front without change activity

家住魔仙堡 提交于 2021-02-18 04:36:41

问题


I want to create notification that when it clicked will bring my app to front but without changing (reload or navigate out) the last activity that was shown.

I tried:

setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT))

But in the new Android 4.3 the application brought to front, but its also start a new instance of MainActivity, and I don't want this.

I want that my app will continue from the last activity that was shown.
How to do that?


回答1:


You don't ever set Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT. That flag is set by Android when it brings the activity to the front. You setting it has no effect.

There's a few ways to do what you want. Check out this answer




回答2:


Working solution for me was :

    //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



回答3:


Add this "Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP" instead of "Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT"

Explained here

Hope this helps.



来源:https://stackoverflow.com/questions/19349421/notification-bring-app-to-front-without-change-activity

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