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.
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
You may create your class, which extends Application with stack or array of your activities. And in onStop() of activities you may change order
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.