[英]Android onCreate() not called with Notification
I have met with interesting intent workflow handling. 我遇到了有趣的意图工作流程处理。 I create a notification like this:
我创建这样的通知:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.abc_textfield_search_default_holo_dark).setContentTitle("Мероприятие изменилось")
.setContentText(eventToShow.valueAt(i).name + " " + eventToShow.valueAt(i).date).setAutoCancel(true);
Intent resultIntent = new Intent(context, MainActivity.class);
resultIntent.putExtra(MainActivity.NOTIFICATION_EVENT_DISPLAY, eventToShow.valueAt(i).id);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.notify(eventToShow.valueAt(i).id, mBuilder.build());
When notificataion is arrived to user, i expect that when he clicks on it MainActivity.class will handle this intent. 通知到达用户后,我希望他单击它时MainActivity.class将处理此意图。 I process information about intent inside
MainActivity.onCreate()
. 我在
MainActivity.onCreate()
处理有关意图的信息。
Everything goes ok if i click on notification outside my app. 如果我在应用程序外部单击通知,一切都会正常。 Activity correctly processing this intent and shows what i expected.
活动正确处理此意图并显示我的期望。
But when i click on notification inside my app there is no new instanse of my MainActivity.class
And onCreate()
is not firing. 但是,当我单击应用程序内的通知时,
MainActivity.class
没有新的实例,并且onCreate()
不会触发。 I thought that it will be created new Instance of MainActivity.class
我以为它将创建
MainActivity.class
新实例
Can you explain me is this a default behavior and how should i handle this situation? 您能解释一下这是默认行为吗,我应该如何处理这种情况?
After testing my app i find next. 测试我的应用程序后,我会找到下一步。
a) MainActivity -> get notification -> Close app -> Open app(MainActivity) -> press notification -> Notification opening new instance of MainActivity as expected. a)MainActivity->获取通知->关闭应用程序->打开应用程序(MainActivity)->按下通知->通知按预期方式打开MainActivity的新实例。
b) MainActivity -> get notification -> press notification -> nothing is happened b)MainActivity->获取通知->新闻通知->什么都没发生
So i was researching about one day to fix that bug and here what i has found. 因此,我正在研究大约一天来修复该错误,并在这里找到了我发现的问题。 I am certainly sure that Android task system can give you headache.
我确信Android任务系统会让您头痛。 It seem's that if Android decides that you are calling your intent from
non-Activity context
it can be replased with new Intent. 似乎如果Android决定您要从
non-Activity context
调用您的意图,则可以用新的意图替换它。
startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK
And this intent
will replace your original intent
and you will not catch any data that you insert to it. 并且该
intent
将替换您的原始intent
并且您不会捕获插入到其中的任何数据。
You can do next to prevent this situation. 您可以执行下一步以防止这种情况。 In your activity you should
Override
next method: 在您的活动中,您应该
Override
下一个方法:
@Override
public void onNewIntent(Intent intent) {
int eventId = intent.getIntExtra(MainActivity.NOTIFICATION_EVENT_DISPLAY, -1);
}
And now you can handle this data 现在您可以处理此数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.