从通知转换到新活动时,请保留父活动的后台堆叠

时间:2018-06-05 17:19:04

标签: android push-notification android-notifications taskstackbuilder

在我的应用中,Section作为一些片段的主机,MainActivity是默认的。

公交流程:fragmentA - > fragmentA

显示fragmentB时,通知会进入状态栏。我点击通知,fragmentB将会启动。

我想,按下后退按钮后,它会将我带回SecondActivity但重新创建fragmentBMainActivity将会显示)。

以下是fragmentA方法:

sendNotification

这是在清单中设置的。

public void sendNotification(Context context) {
  // Creates an explicit intent for an Activity
  Intent resultIntent = new Intent(context, SecondActivity.class);

  // Creating a artifical activity stack for the notification activity
  TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
  stackBuilder.addParentStack(SecondActivity.class);
  stackBuilder.addNextIntent(resultIntent);

  // Pending intent to the notification manager
  PendingIntent resultPending = stackBuilder
        .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

  // Building the notification
  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.ic_launcher) // notification icon
    .setContentTitle("I'm a simple notification") 
    .setContentText("I'm the text of the simple notification") // notification text
    .setContentIntent(resultPending); // notification intent

  // mId allows you to update the notification later on.
  mNotificationManager.notify(10, mBuilder.build());
}

我还添加&#34; singleInstance launchMode&#34; <activity android:name=".MainActivity">   <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondActivity"> <!-- Necessary to the task manager to know who is the parent activity, if you are developing API 16 or higher, you can use: android:parentActivityName=".MainActivity" --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" /> </activity>

我的代码有什么问题?提前谢谢。

0 个答案:

没有答案