par4301 par4301 - 1 month ago 9
Android Question

Resume my android app from Notification bar (and app icon still shows in bar)

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?

Answer