Android:如果将 Activity 带回屏幕,则通知中的 PendingIntent 不会触发 onCreate() [英] Android: PendingIntent from Notification doesn't trigger the onCreate() if bringing Activity back on Screen
问题描述
我猜我对 Intent Flags 有一些误解.我想要做的是,我有一个广播流应用程序,它有两个活动(PlayerApplication 和 SettingsScreen).我有一个在后台运行的用于流式处理的 Service.class,它也包含一个通知(您可以在通知覆盖菜单和 PlayerApplication 中停止/开始播放).如果用户点击通知,PlayerApplicationActivity 应该回到屏幕.
guess I'm having some misunderstandigs with the Intent Flags.. What I'm trying to do is, I'm having a radio streaming applications, which has two Activities (PlayerApplication and SettingsScreen). I have a Service.class for Streaming which runs in the background, which holds a Notification as well (you can stop/start playback in the notification-overlay menu and the PlayerApplication). If the User clicks on the Notification, the PlayerApplicationActivity should be com back to screen.
一切正常,期待以下情况:用户打开 SettingsScreenActivity -> 打开 NotificationOverlayMenu -> 点击 Notification -> PendingIntent 恢复到 PlayerApplicationActivity
Everything works fine, expect the case: User opens SettingsScreenActivity -> opens NotificationOverlayMenu -> clicks at Notification -> PendingIntent resumes to PlayerApplicationActivity
现在,PlayerApplicationScreen 在那里,但我无法点击任何东西.我看到,如果我从通知中恢复,则不会触发我的 PlayerApplication 的 onCreate().但是布局看起来不错(也在 onCreate() 中膨胀了)
Now, PlayerApplicationScreen is there, but I cannot click anything. I see, that the onCreate() of my PlayerApplication isn't triggered, if I resume from Notification. But the layout looks fine (also inflated in the onCreate())
我在 Service.class 中使用了以下 PendingIntent:
I used following PendingIntent in the Service.class:
PendingIntent contentIntent = PendingIntent.getActivity(
this.getApplicationContext(), //Service.class
0,
new Intent(this,PlayerApplicationActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP),
0);
所以实际上 PendingIntent 应该将 Activity(如果已经运行)带回到顶部.我是使用了错误的 Intent 还是我错过了一点?
So actually the PendingIntent should bring the Activity (if already running) back to the top. Am I using wrong Intents or am I missing a point?
我在 Manifest.xml 中声明了 launchMode
and I declared launchMode in Manifest.xml
android:launchMode="singleTask"
推荐答案
我刚刚发现我做错了什么:
I just figured out what I was doing wrong:
为了我的目的,我使用了错误的启动模式.正确的是
singleTop
不是singleTask
我在
而不是
//Manifest.xml
<activity
android:name="..."
android:launchMode="singleTop" >
这篇关于Android:如果将 Activity 带回屏幕,则通知中的 PendingIntent 不会触发 onCreate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!