【问题标题】:return to previous activity not working as intended返回之前的活动未按预期工作
【发布时间】:2016-02-08 16:30:20
【问题描述】:

所以我有一个打开活动的通知:

public void showNotification(View v) {

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.icon);
    builder.setContentTitle("title text");
    builder.setContentText("text");
    builder.setOngoing(true);
    Intent intent = new Intent(this, SecondClass.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(SecondClass.class);
    stackBuilder.addNextIntent(intent);
    final PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);
    final NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NM.notify(0, builder.build());

我所做的活动是一个简单的对话框样式的活动。当我从通知抽屉打开活动时,在任何其他应用程序(如 facebook、whatsapp、chrome 浏览器等)之上,活动按预期打开。 问题是当我尝试关闭它并返回上一个应用程序时。

当我单击返回按钮以关闭对话框样式的活动时,我会直接返回手机的主屏幕,而不是返回上一个应用程序。为什么?

当我点击我在对话框中创建的“关闭”按钮时:

 close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SecondClass.this.finish();

            }
        });

同样的事情发生了:活动结束了,但我要回到主屏幕,而不是上一个应用程序。为什么??

*注意:即使在我完成我的活动之后,之前的应用程序仍在后台运行。

在搜索解决方案时,我发现了这个链接: How to open dialog styled activity from notification without previous activity closing?

但没有给出解决方案。

附:我是android开发的新手,如果可能的话,请让它简单:)

【问题讨论】:

    标签: android


    【解决方案1】:

    您似乎遇到了同样的问题定期活动和通知行动的特殊活动,我的答案在这里:After notification button return to original activity

    【讨论】:

    • 嗨,朋友。如果您检查我发布的代码,您会看到我在您的链接final PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 上确实有您的建议,也许我做错了什么?
    【解决方案2】:

    经过长时间的搜索,我在此链接找到了解决方案: Go back to previous screen on backbutton pressed after responding to notification

    显然我的代码中有 2 个问题。首先,正如链接中解释的那样,我没有使用.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

    第二,根据:http://www.tutorialspoint.com/android/android_notifications.htm 我不应该使用堆栈构建器。堆栈构建器将为已启动的 Activity 包含一个人工返回堆栈。这确保了从 Activity 向后导航会导致您的应用程序离开主屏幕。而这不是我想要达到的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-16
      • 2015-12-03
      • 1970-01-01
      • 2015-08-01
      • 2019-09-15
      • 2018-03-12
      • 2023-04-11
      • 1970-01-01
      相关资源
      最近更新 更多