Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent to resume a previously paused activity (Called from a Notification)

Tags:

I'm developing an app which shows a notification to the user. The notification's objective is to make it easy to the user to return to the activity when the user is in another activity. I am using this code in my app to create and show the notification.

                    notification = new Notification(R.drawable.icon,                             "Notify",                             System.currentTimeMillis());                     notification.setLatestEventInfo(this, "App name",                             "App message",                             PendingIntent.getActivity(                                     this, 0,                                     new Intent(this, Main.class),                                     PendingIntent.FLAG_CANCEL_CURRENT));                     notification.flags |= Notification.FLAG_ONGOING_EVENT;                     nManager.notify(0, notification); 

But when the user taps the notification starts a new instance of the same activity, instead of the one that the user was using before.

I think this has something to do with PendingIntent but I can't find how to make that Intent to resume a previously paused instance of the activity instead of creating a new instance.

Thanks.

like image 287
Jimix Avatar asked Mar 09 '11 15:03

Jimix


People also ask

When a migration activity is paused by the user what methods can be used to resume the activity?

Resume Your Activity When the user resumes your activity from the Paused state, the system calls the onResume() method. Be aware that the system calls this method every time your activity comes into the foreground, including when it's created for the first time.

How do you call an intent to another activity?

Start the Second Activity To start an activity, call startActivity() and pass it your Intent . The system receives this call and starts an instance of the Activity specified by the Intent . Now you need to create the DisplayMessageActivity class in order for this to work.

What is onResume method in Android?

onResume() is called whenever you navigate back to the activity from a call or something else. You can override the onResume method similarly as onCreate() and perform the task.


2 Answers

I found out how to do it. I added the following code:

notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

Now my code looks like this:

    notification = new Notification(R.drawable.icon,             "Notify", System.currentTimeMillis());     notification.setLatestEventInfo(this, "App name",             "App message", PendingIntent.getActivity(this,                     0, new Intent(this, Main.class)                             .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP                                     | Intent.FLAG_ACTIVITY_SINGLE_TOP),                     PendingIntent.FLAG_CANCEL_CURRENT));     notification.flags |= Notification.FLAG_ONGOING_EVENT; 
like image 101
Jimix Avatar answered Sep 24 '22 06:09

Jimix


This helps for me android:launchMode="singleInstance"

<activity         android:name="com.mosis.automatskidnevnik.MainActivity"         android:launchMode="singleInstance"         android:label="@string/app_name" >         ... 
like image 36
Zeljko Ivanovic Avatar answered Sep 20 '22 06:09

Zeljko Ivanovic