Why do we use the TaskStackBuilder?

后端 未结 6 586
难免孤独
难免孤独 2020-12-24 01:12

Why do we use the TaskStackBuilder when creating a notification? I do not get the logic behind it.

Can someone please explain.

public v         


        
6条回答
  •  一向
    一向 (楼主)
    2020-12-24 02:05

    To provide proper navigation.

    1) When the app is launched by App icon (Normal Flow)

    **

    2) When the app is launched by some Notification

    General flow of navigation in your app is MainActivity->DetailActivity

    But sometimes a Notification might directly open the DetailActivity. In this case, pressing the back button in DetailActivity will not lead you to the `MainActivity. It's an EXPECTED BEHAVIOR. However, you can modify this if you want to navigate back to MainActivity.

    How do I do it?

    1) Add android:parentActivityName="com.example.myApp.MainActivity in your Activity

    This feature was added in Android 4.1. So if you want to target older devices. Add a meta-tag ALSO.

    
        
    
    

    2) Use TaskStackBuilder to create a Pending Intent.

    public PendingIntent getPendingIntent(Intent intent){
    
        TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
        taskStackBuilder.addNextIntentWithParentStack(intent);
        PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
    
    }
    

    Now pass this Pending intent to create Notifications.

提交回复
热议问题