I've got an activity that I launch from a notification. I include a back-stack using TaskStackBuilder so that when the user hits the home button (actionbar title button) or uses the back key, it will return to the application. However, it isn't working in that manner, instead hitting back or the actionbar title button always results in the app closing.
For what it's worth, my project structure is organized such that all UI components are in an Android Library (com.be.commotion) and an outside "wrapper" project uses that Library.
Here's how I build the notification:
// This is the activity that will be launched when tapping the notification
Intent intentNotification = new Intent(this, NowPlaying.class);
intentNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Create the back stack so the user can get back to the main activity when pressing the back button
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(NowPlaying.class);
stackBuilder.addNextIntent(intentNotification);
PendingIntent pendingNowPlayingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup the custom notification view
RemoteViews notificationContent = new RemoteViews(getPackageName(), R.layout.notification_now_playing);
....
// Setup the intent that will play/stop music when the stop button is tapped
Intent musicControlIntent = new Intent(this, CommotionMediaPlayer.class);
PendingIntent musicPendingIntent = PendingIntent.getService(this, 0, musicControlIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationContent.setOnClickPendingIntent(R.id.ibMediaControl, musicPendingIntent);
// Update the notification with this info
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(song.text)
.setContentText(song.artist)
.setContentIntent(pendingNowPlayingIntent)
.setLargeIcon(artwork)
.setContent(notificationContent)
.setOngoing(true);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Using the nowPlayingNotificationId allows the notification to be updated with later calls instead of causing a new notification to show up
mNotificationManager.notify(nowPlayingNotificationId, builder.build());
Here's the applicable definitions in my AndroidManifest.xml (for the wrapper project):
<activity android:name="com.be.commotion.ui.StartupActivity"
android:label="@string/app_name"
android:noHistory="true"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.be.commotion.ui.NowPlaying"
android:label="Now Playing"
android:parentActivityName="com.be.commotion.ui.StartupActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.be.commotion.ui.StartupActivity" />
</activity>