Below function displays a notification which pending intents. When the screen is unlock and receive notification, below method will create a notification.
When user tap on notification, it opens first ChildActivity, On backpress, it closes child activity and opens MainActivity->subActivity. on backpress closed subActivity and shows MainActivity.
Subactivity will be started based on the Intent action of MainActivity
In the unlock screen, below method gives above result and its perfect as per my requirement.
public void showNotification(Context context, String extraUri){
final Intent mainActivityIntent = new Intent(context, MainActivity.class);
mainActivityIntent.putExtra(MainActivity.INTENT_EXTRA_ACTIVE, 1232123);
// this will help determine to start subactivity from mainActivity
mainActivityIntent.setAction(MainActivity.MAIN_ACTIVITY_CHANGED);
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
taskStackBuilder.addNextIntent(mainActivityIntent);
final Intent intent = new Intent(context, ChildActivity.class);
intent.putExtra(ChildActivity.EXTRA_URI, extraUri);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
taskStackBuilder.addNextIntent(intent);
final PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("Message Body")
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Problem
while screen is locked and tap on the notification, it ask to unlock the screen, on unlock, it starts the app. but
very first it shows SubActivity, than on back press it shows ChildActivity and on backpress of ChildActivity it shows MainActivity.
So the order of the Activities are changed.
the correct order is MainActivity->SubActivity->ChildActivity (TopMost) (happens in unlocked screen)
Thanks in Advance.