5

I have an android notification bar which uses a remote view. I have 2 buttons on there to play and pause audio and also an icon to return to the application. I want to be able to click the icon (or anywhere but on the 2 buttons) and return to the application. Here is my code

Intent returnIntent = new Intent(_context, SplashScreenActivity.class);
returnIntent.setAction(Intent.ACTION_MAIN);
returnIntent.addCategory(Intent.CATEGORY_LAUNCHER);

PendingIntent remoteViewPendingIntent = PendingIntent.getActivity(_context,1, returnIntent,0);
remoteView.setOnClickPendingIntent(R.id.btnAppIcon, remoteViewPendingIntent);

This all works fine in the emulator on Android 4.1. When the icon in the notification is pressed it returns successfully to the app. However on Android 4.0.3 on a Samsung S3 the Activity launches in the background but the notification screen is not hidden. I want the notification screen to be cleared once the icon is selected. I have tried using the Notification.FLAG_AUTO_CANCEL in the notification but that did not fix the issue. Any help would be greatly appreciated.

user706593
  • 51
  • 5

1 Answers1

3

.setOnClickPendingIntent behavior varies on different MFGs and versions as you've experienced.

To accomplish what you want and auto collapse the notification window automatically (without using reflection), you have to set a "global" pending intent for the notification view. You will need to include your R.id.btnAppIcon in that "catch all" intent view as well.

Example of the "global/default" notification pending intent:

contentIntent = PendingIntent.getActivity(YourClass.this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;

The "general" notification intent when used with PendingIntent.getActivity will close the notification window on any device (as I've observed).

logray
  • 2,332
  • 1
  • 29
  • 30