0

I have a custom application that has multiple activities running in full screen (SplashActivity, MainActivity, InfoActivity, and so on...). I'm currently working on notifications and i'm trying to have the following behavior: if the application is running and put to background and the user clicks the notification, the last activity that was running resumes. If the user is not running the application, then the SplashActivity is launched. Is there a way to have this behavior? I'm using the following code

android.app.NotificationManager notificationManager = (android.app.NotificationManager) instance.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(instance, SplashActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent contentIntent = PendingIntent.getActivity(instance, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(instance);

    builder.setSmallIcon(iconId);
    builder.setContentTitle(title);
    builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
    builder.setContentText(message);
    builder.setContentIntent(contentIntent);

    notificationManager.notify(0, builder.build());

If i set the activity to SplashActivity.class it will open that activity whether the application was running or not, so that would only be right if the application was not running.

xlar8or
  • 601
  • 9
  • 18
  • Check [this link](http://stackoverflow.com/questions/10955484/change-notification-intent-in-android) – Merlevede Mar 04 '14 at 18:37
  • this helped me http://stackoverflow.com/questions/3305088/how-to-make-notification-intent-resume-rather-than-making-a-new-intent/39482464#39482464 – TharakaNirmana Sep 14 '16 at 07:47

2 Answers2

0

This behavior is the default behavior that the OS will perform on your application Unless the OS needs memory and your application in the backStack . In this case the OS will force close your application if it was in the background.

So, you cannot depend on the OS in this situation. You can handle it manually using the SharedPreference to store your current state of the application and check it when your notification is being clicked or your application is being opened.

Here is an example of how to use the SharedPreference to keep a state of your app

Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
  • I've updated my question to state that my default behaviour does not do that, because i have to specify an activity to launch in the intent when i click the notification, so it will always open that activity instead of resuming the last activity opened before the application was put in the background, in the case where the application was already running. – xlar8or Mar 04 '14 at 20:07
0

You may create your class, which extends Application with stack or array of your activities. And in onStop() of activities you may change order

Artem
  • 1,566
  • 1
  • 10
  • 15