Saturday, May 3, 2014

Android: adding backstack to the Pushnotification Activity

When User has an action on notifications, the activity is launching on tap of the notification but on press back application closes normally.

We can open activity rather than closing application on back press.

How to achieve this?

By building back stack to the application, when coming from the notification.
So, we need to know about the parentActivityName and Create back stack when starting the activity

Specify parent activities in the manifest

Beginning in Android 4.1 (API level 16), we can declare the logical parent of each activity by specifying the android:parentActivityName attribute in the <activity> element.  

<activity android:name="com.vardhan.pushnotificationtest.HomeActivity" />
<activity android:name="com.vardhan.pushnotificationtest.PushNotificationActvity"
          android:parentActivityName="com.vardhan.pushnotificationtest.HomeActivity"  >
</activity>


If your app supports Android 4.0 and lower, include the Support Library with your app and add a element inside the <activity>. Then specify the parent activity as the value for

<activity android:name="com.vardhan.pushnotificationtest.HomeActivity" />
<activity android:name="com.vardhan.pushnotificationtest.PushNotificationActvity" >
         
          <meta-data
                   android:name="android.support.PARENT_ACTIVITY"
             android:value="com.vardhan.pushnotificationtest.HomeActivity" />
</activity>

Create back stack when starting the activity

Adding activities to the back stack begins upon the event that takes the user into your app. That is, instead of calling startActivity(), use the TaskStackBuilder APIs to define each activity that should be placed into a new back stack. Then begin the target activity by calling startActivities(), or create the appropriate PendingIntent by calling getPendingIntent().

// Intent for the activity to open when user selects the notification
Intent displayIntent = new Intent(this, PushNotificationActvity.class);

// Use TaskStackBuilder to build the back stack and get the PendingIntent
PendingIntent contentIntent = TaskStackBuilder.create(this)
                   // add all of SecondActvity's parents to the stack,
                   // followed by SecondActvity itself
                   .addNextIntentWithParentStack(displayIntent)
                   .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

// Creates notification Builder, set small icon, title, style and content to the builder.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                   this).setSmallIcon(R.drawable.ic_stat_gcm)
                   .setContentTitle("GCM Notification")
                   .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                   .setContentText(msg);

// Puts the PendingIntent into the notification builder
mBuilder.setContentIntent(contentIntent);

// Notifications are issued by sending them to the
// NotificationManager system service.
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// Builds an anonymous Notification object from the builder, and
// passes it to the NotificationManager
mNotificationManager.notify(id, mBuilder.build());

The resulting PendingIntent specifies not only the activity to start (as defined by displayIntent), but also the back stack that should be inserted into the task (all parents of the PushNotificationActvity defined by displayIntent). So when the PushNotificationActvity starts, pressing Back navigates backward through each of the PushNotificationActvity class's parent activities.

Note: In order for the addNextIntentWithParentStack() method to work, you must declare the logical parent of each activity in your manifest file, using the android:parentActivityName attribute (and corresponding <meta-data> element) as described above.

Why PendingIntent?
Intent English meaning is intended; pending said the impending advent of things. 
The PendingIntent specifies an action to take in the future. 

By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself (with the same permissions and identity). As such, you should be careful about how you build the PendingIntent

Why to use addNextIntentWithParentStack?
Add a new Intent with the resolved chain of parents for the target activity to the task stack.
This is equivalent to calling addParentStack with the resolved ComponentName of nextIntent (if it can be resolved), followed by addNextIntent with nextIntent.



Thanks for reading :) 
Whether this post is helpful?

Have something to add to this post? If you have any other quick thoughts/hints that you think people will find useful? Share it in the comments.

No comments :

Post a Comment