I want to resume my app, exactly at where I left from notification icon in bar. Then I used this code:
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_speed)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.txt_welcome))
.setAutoCancel(true).setDefaults(Notification.DEFAULT_LIGHTS).setWhen(System.currentTimeMillis())
.setOngoing(true);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());
It works well, the issue is when I press the icon in notification bar, it disappears, I need it to stay there.
Of course, if I delete these three lines:
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in manifest.xml
<activity android:name=".MainActivity"
android:launchMode="singleInstance"
android:screenOrientation="portrait"/>
the icon won't disappear after clicking on it, but I always redirect to MainActivity, not the same place I left (when app got into pause).
any idea?
Try setting :
setAutoCancel(false)
Reference : https://developer.android.com/reference/android/app/Notification.Builder.html#setAutoCancel(boolean)
Hope it helps !
Edit :
As per discussion, need to set in manifest :
android:launchMode="singleTop"
This will deliver new intent to "onNewIntent" function which can be used to do further actions in case your activity was already running.