Notification中使用TaskStackBuilder类处理打开的Activity点击back键后的效果

java代码:

        mBuilder = new NotificationCompat.Builder(this).setContent(view)
                .setSmallIcon(R.drawable.icon).setTicker("新资讯")
                .setWhen(System.currentTimeMillis())
                .setOngoing(false)
                .setAutoCancel(true);
        Intent intent = new Intent(this, NotificationShow.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(NotificationShow.class);
        stackBuilder.addNextIntent(intent);
        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingIntent);

AndroidManifest.xml中设置android:parentActivityName属性值.

        
        

这样, 当通知栏被点击后, 显示NotificationShow Activity, 当点击back时, 不会回到桌面, 而是进入app的主页面.

之前要想达到同样目的, 需要在Activity中记录一个flag值, 用来标记这个Activity是从notification打开的, 然后在Activity的 onBackPressed()中做处理导航进入MainActivity.

----DONE.---------

你可能感兴趣的:(Notification中使用TaskStackBuilder类处理打开的Activity点击back键后的效果)