【问题标题】:Go back to previous screen on backbutton pressed after responding to notification响应通知后按下返回按钮返回上一个屏幕
【发布时间】:2014-01-02 17:12:38
【问题描述】:

我正在使用类似于Creating a simple notification 的代码来创建和显示来自网络调用的通知。

问题是我希望响应通知的活动执行它的业务,然后单击后退按钮,将先前活动的活动放回前台,并且它的后退堆栈完好无损。这与之前的活动活动是我的应用程序的一部分还是其他人无关。

目前它正在关注生成的TaskStackBuilder。引导它备份应用程序层次结构并到主屏幕。这是一个糟糕的 UI 设计,因为它破坏了使用该设备的任何人的工作流程,迫使他们手动返回他们的应用程序并花费更多的按钮点击次数。它也相当不直观。

According to the official desing guidelines this is how it should work. The implementation I linked to higher up makes back button have the same functionality as up button should have

这也是在众多其他应用程序中实现它的常用方法,包括官方 google 应用程序(想到 google-play 更新通知),因此必须有一个相对标准的方法来执行此操作。

【问题讨论】:

    标签: android android-notifications back-button


    【解决方案1】:
    Intent intent = new Intent(getApplicationContext(), MainActivity.class)
    //add Intent.FLAG_ACTIVITY_CLEAR_TASK flag this will clear all activitys and 
    //launched activity at top. Means no other activity of this application will be running
    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
         or
    // add Intent.FLAG_ACTIVITY_MULTIPLE_TASK which start one more task your applications 
    //   where activity will not be cleared;
      .addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
      Notification n  = new Builder(context.getApplicationContext())
                     .setContentTitle("simple notification title")
                     .setContentText("simple message")
                     .setContentIntent(pendingIntent)
                     .setAutoCancel(true)
                     .addAction(R.drawable.ic_launcher, "And more",pendingIntent ).build();
    
    
      NotificationManager notificationManager =(NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
                                               notificationManager.notify(0, n);
    

    【讨论】:

    • 改变你的行 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); to.addFlags(Intent.Intent.FLAG_ACTIVITY_MULTIPLE_TASK);我会接受答案,因为它让我走上了正轨。 Intent.FLAG_ACTIVITY_CLEAR_TASK 适用于其他应用程序,但在您返回时会关闭您自己的应用程序。 FLAG_ACTIVITY_MULTIPLE_TASK 正是我所要求的。谢谢你。将pendingIntent 更改为pIntent 也可能是一个好主意。相当明显的错字,但有人可能会感到困惑
    • 感谢您的建议,并非常感谢您分享有价值的信息。快乐的编码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多