Android通知导航backstack

时间:2015-11-02 10:24:47

标签: android android-activity android-notifications

您好团队我在通知背景中遇到问题

说我有4项活动A1,A2,A3,A4

我有一个意向A2的通知,假设我们在A4中通过A1 - > A2 - > A3 - > A4,现在我收到通知,而点击通知我打算A3,当我在这个场景中按下我的导航堆栈时,我的导航堆栈被清除,应用程序正在关闭。

但我在这种情况下的预期行为是它应该转到A4和A3,A2,A1按下后退按钮

代码

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
    mBuilder.setSmallIcon(R.mipmap.ic_launcher);
    mBuilder.setContentTitle("Notification Alert, Click Me!");
    mBuilder.setContentText("Hi, This is Android Notification Detail!");
    Intent resultIntent = new Intent(context, SecondActivity.class);
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(FirstActivity.class);

// Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    //PendingIntent resultPendingIntent = PendingIntent.getActivity(context,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

// notificationID allows you to update the notification later on.
        mNotificationManager.notify(14, mBuilder.build()); 

清单文件

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".FirstActivity" >
    </activity>
    <activity
        android:name=".SecondActivity"
        android:label="@string/title_activity_second"
        android:parentActivityName=".FirstActivity"
        android:theme="@style/AppTheme.NoActionBar" >
    </activity>
    <activity
        android:name=".ThirdActivity"
        android:label="@string/title_activity_third"
        android:theme="@style/AppTheme.NoActionBar" >
    </activity>
</application>

1 个答案:

答案 0 :(得分:0)

您使用addParentStack()的错误参数。您应该将通知中打开的活动视为参数,而不是它的父级。所以将代码更改为:

stackBuilder.addParentStack(SecondActivity.class);

如果您的通知是针对A2的,则它将始终打开A2,而不是A3。

API等级16中引入了

android:parentActivityName。如果您在较旧的手机上运行此功能,则必须将父活动定义为元数据:

<activity
        android:name=".SecondActivity"
        android:label="@string/title_activity_second"
        android:parentActivityName=".FirstActivity"
        android:theme="@style/AppTheme.NoActionBar" >
 <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".FirstActivity" />
</activity>