android - 响应通知后按下后退按钮返回上一屏幕

标签 android android-notifications back-button

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

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

目前它跟随生成的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 更新通知),因此必须有一种相对标准的方法来执行此操作。

最佳答案

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);

关于android - 响应通知后按下后退按钮返回上一屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20888013/

相关文章:

android - 如何使用 Parcel 在 Intent 之间传递外部对象

android - 使用 Android : how do I prevent underling views from drawing on top of my custom view? 在 View 剪辑边界之外绘制时

android - 是否可以减少 Android 中显示的通知时间?

Android:有些通知可以取消,有些不可以

ios - backBarButtonItem 显示图像不正确

android - 以编程方式截取屏幕截图不会捕获 surfaceVIew 的内容

android - 线圈单元测试 - 如何做到这一点?

android - PendingIntent.getBroadcast lint 警告

android - 如何在 android 中使用不同的应用程序实现对我的应用程序的导航?

android - 为什么我的后退按钮在关闭对话框 fragment 后不能正常工作