通知打开活动,按下后退按钮,主要活动是否打开?

Ric*_*ral 8 notifications android back-button android-intent android-pendingintent

我能描述问题的最好方法是这样的:

  1. 在引导时创建通知(带a BroadcastReceiver).
  2. 我的应用程序主要活动已打开,并且按下主页按钮(应用程序仍在后台运行,直到系统关闭它).
  3. 我按下状态栏并按下之前在启动时创建的通知.
  4. 开始了一些与主要活动不同的活动.
  5. 我按下后退按钮,显示主要活动.

我该如何防止最后一步?我想要的后退按钮是回到原来的位置,这是主屏幕(包含所有小部件和应用程序图标的桌面).我的应用程序的主要活动应该是在后台运行,为什么用后退按钮调用它?

如果相关,我创建通知的代码如下:

public void createNotification(int notifyId, int iconId, String contentTitle, String contentText) {
    Intent intent = new Intent(mContext, NewNoteActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(AgendaNotesAdapter.KEY_ROW_ID, (long)notifyId);

    PendingIntent contentIntent = PendingIntent.getActivity(mContext, notifyId, intent, 0);

    Notification notification = new Notification(iconId, contentTitle, 0);
    notification.setLatestEventInfo(mContext, contentTitle, contentText, contentIntent);

    mNotificationManager.notify(notifyId, notification);
Run Code Online (Sandbox Code Playgroud)

我试图添加几个标志组合,intent但他们都没有解决我的问题...建议?

小智 20

对谁来说仍然需要回答.看起来这就是你想要实现的目标:

Activity从通知开始时,您必须保留用户的预期导航体验.单击" 返回"应该让用户通过应用程序的正常工作流程回到主屏幕,单击" 最近"应将"活动"显示为单独的任务.

http://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse

您的情况是 - 设置常规活动PendingIntent

请参阅链接中的完整步骤.基本上您需要:
1.ActivityAndroidManifest.xml中定义层次结构

<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".ResultActivity"
    android:parentActivityName=".MainActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity"/>
</activity>
Run Code Online (Sandbox Code Playgroud)


2.创建一个回栈基础上,Intent启动了Activity:

...
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
Run Code Online (Sandbox Code Playgroud)

  • 这是最好的答案 (2认同)

Sho*_*oom 5

有同样的问题.对我有用的是在清单中为通知启动的活动设置以下属性:

android:taskAffinity=""

这基本上将通知活动的亲和力与主要活动分开,因此操作系统感觉不需要"返回"主活动,而只是返回到之前的任何内容,例如"桌面".


Dha*_*dra 4

我从你的问题中知道你的问题是在以下步骤中

1.启动完成后创建通知

2.Main Activity 将在启动时调用

3.您按下了主页按钮,因此主要活动将停止但不会破坏

4.您单击状态栏上的通知,以便您application will be resume的后堆栈中已经有主要活动,并且通知将创建新活动,就像您在问题 NewNoteActivity 活动中提到的那样,该活动将被推送到后堆栈上。所以在这一步you have two activities in back stack

5.您按下后退按钮,然后last activity will be destroymain activity will be resume想转到主页屏幕。

所以我从你的问题中知道你的问题是在以下步骤中

1.启动完成后创建通知

2.Main Activity 将在启动时调用

3.您按下了主页按钮,因此主要活动将停止但不会破坏

4.您单击状态栏上的通知,以便您application will be resume的后堆栈中已经有主要活动,并且通知将创建新活动,就像您在问题 NewNoteActivity 活动中提到的那样,该活动将被推送到后堆栈上。所以在这一步you have two activities in back stack

5.您按下后退按钮,以便last activity will be destroy您的main activity will be resume但是当您从 NewNoteActivity 活动中按下后退按钮时,您想打开主页活动。

因此,解决方案是,当您从 NewNoteActivityactivity 按下后退按钮时,您将使用Intent.FLAG_ACTIVITY_CLEAR_TOP标志再次启动主要活动,以便您的主要活动将重新创建并接收onNewIntent()方法,以便您可以获得标志并完成主要活动

例如

@Override
    public void onBackPressed() {
        Intent i = new Intent(this, Main.class);
    i.putExtra("exit", true);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    super.onBackPressed();
    }
Run Code Online (Sandbox Code Playgroud)

现在您必须onNewIntent()在 Main 活动中实现方法

从 NewNoteActivity 活动按下后退按钮时,您的 Main 活动将调用onNewIntent()方法,因此在该方法中您必须获取从 NewNoteActivity 活动传递的标志变量。如果您获得标志并且为 true,则只需完成 Main 活动即可获得主屏幕。

编辑

您是说,您打开了 A、B 或 C 中的任何一个活动,并且您按下了后退按钮,以便该活动将被关闭。如果当时堆栈中只有一个活动,您的应用程序将被关闭意味着您将进入主屏幕。但是,如果您有多个活动并且按下后退按钮,则堆栈中至少有一个活动,现在您单击通知,以便这将打开与您的通知关联的新活动,以便该活动将现在,如果您按下后退按钮,如果您没有修改该活动中的 onBackPressed() 方法,则与您的通知关联的最后一个活动将被关闭,然后它将检查后退堆栈是否有任何活动在后退堆栈中这样活动将恢复,或者如果返回堆栈中没有活动,那么您的应用程序将关闭,您将进入主屏幕