You might be importing android.app.TaskStackBuilder rather than android.support.v4.app.TaskStackBuilder - check your imports.
TaskStackBuilder - backwards compatible or not?
Question
I'm setting up a notification following the example here. My app is compatible with API 15 and ulterior versions. This is my code:
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// send notification
// API < 16 so have to use compat
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
//.setSmallIcon(context.getResources().getDrawable(icon_id_here))
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, UpcomingTest.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(UpcomingTest.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
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(123, mBuilder.build());
}
When reading the information here about TaskStackBuilder, it says "TaskStackBuilder provides a backward-compatible way to obey the correct conventions around cross-task navigation on the device's version of the platform.". However I'm getting an error that says that I need to target minimum API 16 in order to use TaskStackBuilder. Is there anything else I need to do to actually make it backward-compatible?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow