Pages

20 Feb 2014

Alternative to Intent.FLAG_ACTIVITY_CLEAR_TASK

I want to use Intent.FLAG_ACTIVITY_CLEAR_TASK to start my app from a notification on Android's status bar like it's restarted, but as you may know, Intent.FLAG_ACTIVITY_CLEAR_TASK requires Android API 11, so I found an alternative for it. Basically, just replace:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);

with

ComponentName cn = intent.getComponent();
Intent restartIntent = IntentCompat.makeRestartActivityTask(cn);
PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, restartIntent, PendingIntent.FLAG_UPDATE_CURRENT);

And it works as expected ;)

No comments:

Post a Comment

Note: only a member of this blog may post a comment.