How to restore previous activity by clicking on notification - android

When my notification goes off I want to restore the activity that was put into the background, not start a new activity. I've seen some answers about using FLAGS but I don't know how to implement it
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | INTENT.FLAG_ACTIVITY_SINGLE_TOP);
Where do I put this in my code? I tried but it didn't work. Please help!
ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) getSystemService(ns);
icon = R.drawable.icon;
tickerText = "Short Msg";
when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
context = getApplicationContext();
contentTitle = "MyApp";
contentText = "Reopen App";
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationIntent = new Intent(this, StartTimer.class);
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

Figured it out, set the Activity to SingleTop or SingleInstance in Android Manifest, then instead of creating a new activity it just reopen the one still active.

Note that answer marked as correct is not completely correct as "singleTop" still may create multiple instances of your activity under certain conditions.
The launch modes that really are garanteed to create a UNIQUE instance of your activity at any condition are "singleTask" and "singleInstance".
These two options creates one and only task for your activity being the root of the task, with the difference that "singleInstance" don't allow other activities on top of yours, while "singleTask" does.
Source:
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

Related

Launch An Activity Afresh(Don't Retain State) On A Notification Click

I want to launch an Activity(Afresh, Without any previously retained state) on a notification click. IF this Activity is already in the stack, bring this to top but without any previous state.
So Far i have tried setting Flags to Passed Intent.
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|INTENT.FLAG_ACTIVITY_SINGLE_TOP)
But They Don't serve the purpose. Any suggestions how i could achieve this.
If you remove |Intent.FLAG_ACTIVITY_SINGLE_TOP from your launch this should restart the activity.
Specifying |Intent.FLAG_ACTIVITY_SINGLE_TOP in the launch flags causes the existing instance to be reused. If you remove it the existing instance will be finished and a new instance will be launched.
Here You Go.
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, HomeActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
to open application in anystate.
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
startActivity(LaunchIntent);

How to prevent an app opened repeat

I use the following code to add an icon to Notification status, a user can click the icon to open the app ui.SMSMain.class, and the two ui.SMSMain.class apps will be opened if the user click the icon two times.
I hope the app only can be opened one time, how can I do?
private static void ShowNotification() {
NotificationManager notificationManager = (NotificationManager) myContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.smsforward,
myContext.getString(R.string.app_name),
System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
CharSequence contentTitle =myContext.getString(R.string.NotificationTitle);
CharSequence contentText = myContext.getString(R.string.NotificationContent);
Intent notificationIntent = new Intent(myContext, ui.SMSMain.class);
PendingIntent contentItent = PendingIntent.getActivity(myContext, 0,
notificationIntent, 0);
notification.setLatestEventInfo(myContext, contentTitle, contentText,contentItent);
notificationManager.notify(NotificationID, notification);
}
add launchMode attribute to your activity in the manifest as SingleTop
<activity
android:name="Tracking"
android:label="#string/app_name"
android:launchMode="singleTop">
....
this will allow only one instance of the activity, also it is better to use flag PendingIntent.FLAG_UPDATE_CURRENT in your pendingIntent
PendingIntent contentItent = PendingIntent.getActivity(myContext, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
though, I am not sure if 0 actual refer to UPDATE_CURRENT
Update
you may need to choose among singleTop or singleTask according to your needs
for singleTop: if an instance of activity already exists at the top of the current task and system routes intent to this activity, no new instance will be created
for singleTask: A new task will always be created and a new instance will be pushed to the task as the root one. However, if any activity instance exists in any tasks, the system routes the intent to that activity instance through the onNewIntent() method call.
for more information about LaunchMode.
I believe that singleTop suits your problem better.

Resuming Android app from Notification

How do I resume an Android app from Notification? I want to be able to resume the instance from where it left, with all bounded Services, and settings etc. Instead of a complete new instance. This is my code:
String ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager)
getSystemService(ns);
Intent notificationIntent = new Intent(this, MeasurementsStarter.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations
// above
notification = new Notification(R.drawable.ic_launcher, "Metingen draaien",System.currentTimeMillis());
notification.setLatestEventInfo(getBaseContext(), "Metingen ManDown", "Metingen Mandown draaien nog",
contentIntent);
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
I kind of found an answer, I manually set the data from all of my textFields etc. and just put my bindService in my onCreate() method. It's propably not the most graceous way, but it works nonetheless

android notification issue

I am working on an Android application.The app is like native sms application.I am facing a problem in notification part. In native app, they handle the notification in the following way.
1)if messages from an specific number ,then the click on the notification will lead to the chat page of the corresponding contact and the notification will clear.
2)if messages from the different number, then the click on the notification will lead to home page and notification won't clear.
I had done the first part and I don't have any idea to do the second part.
Is there any way to leave the notification part without clear and call an intent.?
you have to add a flag:
notification.flags |= Notification.FLAG_ONGOING_EVENT;
here a whole example:
private static final int NOTIFICATION_EX = 0;
private NotificationManager notificationManager;
...
in onCreate:
notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.youricon;
CharSequence tickerText = "Sticky notification";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Sticky notification";
CharSequence contentText = "...click here and it wont go away...";
Intent notificationIntent = new Intent(this, mainmenu.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle,
contentText, contentIntent);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(NOTIFICATION_EX, notification);
the intent you can adjust to whatever you like.
You will have to create two different kind of notification, the first one you have already implemented, and the second one you can just copy the first one and check if the number is a different one use the second notification and set the flags to not clear notification2.flags |= Notification.FLAG_ONGOING_EVENT; on click with the HomeActivity in the PendingIntent which has Intent:

Android Status Bar Notification: make it un-clearable and have it return to app (not start a new instance)

I am developing an Android app and I want a status bar notification that cannot be cleared by the user. Does anyone know how to do that? I've seen them with apps like Skimble where a non-clearable notification appears while using the application.
Also, I'd when the user clicks/presses the notification, I want it to return to the app instance that is already running. Right now it starts a new instance. Again like the Skimble app, I just want to return to the already running instance of the app.
Thanks.
I found a solution to this problem. I used FLAG_ONGOING_EVENT to make the notification persist. Here's the code:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.mypic;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags = Notification.FLAG_ONGOING_EVENT;
Context context = getApplicationContext();
CharSequence contentTitle = "Title Text";
CharSequence contentText = "Content text.";
Intent intent = new Intent(this, MyClass.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
Here is the solution for the NotificationCompat.Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("MyApp")
.setContentText("MyMsg")
.setOngoing(true);
More at the Google docs.
notification.setLatestEventInfo is deprecated so you have to use the Notification.builder and if your using 2.X and lower API you have to use NotificationComacpt.Builder and this is not applicable and I couldn't make my notification stays. so I used setDeleteIntent and pointed to the same activity and on resume method just call the block of code which shows your notification. So it is kinda work around.

Categories

Resources