5

I am creating like timer application and when I start timer I have option to go to android Home or start any other activity .

When I start timer I set a notification bar icon and if i use some other application (mean go from started timer activity) and now I need to go to back to my previously started timer activity by clicking on notification icon ???

When I click I am starting a new instance timer activity , not the previously started timer activity ! , and if I then click back button it show me a previously timer activity ..

Question is: How to call previously started activity trough notification bar , not to start new instance of that activity ??

This is sample of my code below :

private void notificationBar()
{
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    int icon = R.drawable.ico;
    CharSequence tickerText = "some title...";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);
    Context context = getApplicationContext();
    CharSequence contentTitle = "some app title";
    CharSequence contentText = "...some info !";
    Intent notificationIntent = new Intent(this, main.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,  PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);


    mNotificationManager.notify(NOTIF_ID, notification);

}    
private void notificationClose(int notifID)
{
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    mNotificationManager.cancel(notifID);

}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Nezir
  • 6,727
  • 12
  • 54
  • 78

2 Answers2

14

I found an answer it's about flags : Android: new Intent() starts new instance with android:launchMode="singleTop"

Intent intent= new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Community
  • 1
  • 1
Nezir
  • 6,727
  • 12
  • 54
  • 78
2

I'm not certain I understand what you mean.

I suppose you could add an extra in the intent to specify which notification exactly was used to invoke your app. Does this help at all?

Emmanuel
  • 16,791
  • 6
  • 48
  • 74
  • 1
    Hi , Manuel I have this code and it's almost work as supposed . Only thing is that click on notification bar does not open/show previously started activity ? it open new instance of that activity ? Is it clear now ? have activity timer , timer started i create notification icon , i do some other application and after while I click on notification bar and I need to open a timer activity which time ticking , not a new instance of fresh activity :) – Nezir Nov 03 '10 at 16:40
  • 2
    In that case, check out the launchMode attribute for activity: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode I think you want to use singleTop. Does this help? – Emmanuel Nov 04 '10 at 15:25