活动开了两次 [英] Activity opened twice

查看:63
本文介绍了活动开了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用城市飞艇进行推送通知的应用程序.当通知到达并且用户单击它时,我的应用程序中的活动A应该打开并执行某些操作.

I have an application that uses Urban Airship for push notification. When a notification arrives and the user clicks on it, activity A in my application should open and do something.

我已经按照BroadcastReceiver >在文档中,并且几乎正常工作.

I've installed the BroadcastReceiver as is shown in the docs, and it's almost working.

  1. 当我的应用程序位于前台时,我完全不会让用户看到通知,而只是自动处理它.
  2. 当我的应用程序完全不运行时,活动打开就很好了.
  3. 当我的应用程序在后台运行时(通常在A是最高活动时发生),将创建第二个活动A实例.
  1. When my app is in the foreground I don't let the user see the notification at all, and just handle it automatically.
  2. When my app is not running at all, the activity opens up just fine.
  3. When my app is in the background (which always happens when A is the top activity), a second instance of Activity A is created.

这当然是一个问题.我不想要两个A活动,我只想要其中一个.以下是相关的BroadcastReceiver代码:

This is, of course, a problem. I don't want two A activities, I just want one of them. Here's the relevant BroadcastReceiver code:

@Override
public void onReceive(Context ctx, Intent intent)
{
    Log.i(tag, "Push notification received: " + intent.toString());
    String action = intent.getAction();
    int notificationId = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, -1);
    if(action.equals(PushManager.ACTION_NOTIFICATION_OPENED))
    {
        Intent intentActivity = new Intent(ctx, ActivityA.class);
        intentActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        UAirship.shared().getApplicationContext().startActivity((intentActivity);
    }
}

更新: 当用户在活动A上按Back时,我试图通过调用System.exit(0)来绕过此错误.该过程结束了,但随后立即重新启动!在第二个实例中,不会再次调用我的BroadcastReceiver.发生什么事了?

UPDATE: I tried to bypass this bug by calling System.exit(0) when the user presses Back on Activity A. The process ended, but then it was restarted immediately! My BroadcastReceiver is not called again in the second instance. What's happening?

更新2: @codeMagic要求提供有关应用程序和活动A的更多信息.

UPDATE 2: @codeMagic asked for more information about the app and activity A.

此应用程序可让其用户查看某些项目并对其发表评论.启动应用程序时将启动活动A.如果用户的会话不再有效,则将启动登录"活动.用户登录后,活动A将再次变为活动状态.仅有一个没有要审查的项目"消息和一个立即尝试"按钮.

This app lets its user review certain items and comment on them. Activity A is started when the app is launched. If the user's session isn't valid any more, a Login activity is started. Once the user logs in, activity A becomes active again. A only has a "No items to review" message and a "Try now" button.

用户登录后,只要有新项目可供查看,服务器就会开始发送推送通知.当应用收到通知时,活动A访问服务器并获取下一个要检查的项目.该项目显示在活动B中.将评论提交到服务器后,活动B完成,活动A再次成为最重要的活动.

When the user logs in, the server starts sending push notifications whenever a new item is available for review. When the app gets the notification, activity A accesses the server and gets the next item to review. The item is shown in activity B. Once the review is submitted to the server, activity B finishes and activity A is again the top activity.

服务器知道用户何时评论某项(因为活动A提取了该信息),并且在提交评论之前不发送推送通知-这意味着如果用户未登录或如果用户正在查看活动B.

The server knows when a user is reviewing an item (because activity A fetched it), and doesn't send push notifications until the review is submitted - meaning a notification can't come if the user isn't logged in or if the user is viewing activity B.

虽然我同意这里存在微妙的比赛条件,但这并不是造成我看到的问题-在测试中,我100%肯定没有比赛条件-推送通知仅在之后发送活动A再次变为活动状态.

While I agree there is a subtle race condition here, it is not causing the problem I'm seeing - in testing I am 100% positive there's no race condition - the push notification is only sent after Activity A becomes active again.

推荐答案

解决方案是在AndroidManifest.xml中的活动中添加launchMode='singleTask'.结果,调用了同一活动实例的onNewIntent而不是新的活动.

The solution was to add a launchMode='singleTask' to the activity in AndroidManifest.xml . As a result, instead of a new activity, onNewIntent of the same activity instance is called.

这篇关于活动开了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆