[android-developers] Switch to already opened application from Notification

2011-01-27 Thread Tina
Hi, all
I have service and application. In some cases my service creates
Ongoing Notification in the Status bar. When user click it - my
application started. It is good, but sometimes my application is
already opened at this moment,

Tell me, please, what should I do to _switch_ to my application if it
is already opened, istead of creating new activity? I want to get the
same behavior as when I press application icon from the Application
list.


From manifest:
activity android:name=com.myapp.MyAppMainActivity
android:launchMode=singleTop
intent-filter
action 
android:name=android.intent.action.MAIN /
category 
android:name=android.intent.category.LAUNCHER /
/intent-filter
/activity


Using code:
public static void startNotification(Context context, int resID, int
iconID, int titleTextID, int contentTextID) {

NotificationManager mNM =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence text = context.getText(contentTextID);

Notification notification = new Notification(iconID, text,
System.currentTimeMillis());

Intent activityToStart = new Intent(context,
MyAppMainActivity.class);
activityToStart.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);

PendingIntent contentIntent =
PendingIntent.getActivity(context, 0, activityToStart, 0);
notification.setLatestEventInfo(context,
context.getText(titleTextID), text, contentIntent);
mNM.notify(resID, notification);
}

With greate hope,
Tina

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Switch to already opened application from Notification

2011-01-27 Thread Mark Murphy
Instead of:

Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY

try:

Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP

On Thu, Jan 27, 2011 at 11:44 AM, Tina tina...@gmail.com wrote:
 Hi, all
 I have service and application. In some cases my service creates
 Ongoing Notification in the Status bar. When user click it - my
 application started. It is good, but sometimes my application is
 already opened at this moment,

 Tell me, please, what should I do to _switch_ to my application if it
 is already opened, istead of creating new activity? I want to get the
 same behavior as when I press application icon from the Application
 list.


 From manifest:
                activity android:name=com.myapp.MyAppMainActivity
 android:launchMode=singleTop
                        intent-filter
                                action 
 android:name=android.intent.action.MAIN /
                                category 
 android:name=android.intent.category.LAUNCHER /
                        /intent-filter
                /activity


 Using code:
 public static void startNotification(Context context, int resID, int
 iconID, int titleTextID, int contentTextID) {

                NotificationManager mNM =
 (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        CharSequence text = context.getText(contentTextID);

        Notification notification = new Notification(iconID, text,
 System.currentTimeMillis());

        Intent activityToStart = new Intent(context,
 MyAppMainActivity.class);
        activityToStart.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |
 Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);

        PendingIntent contentIntent =
 PendingIntent.getActivity(context, 0, activityToStart, 0);
        notification.setLatestEventInfo(context,
 context.getText(titleTextID), text, contentIntent);
        mNM.notify(resID, notification);
    }

 With greate hope,
 Tina

 --
 You received this message because you are subscribed to the Google
 Groups Android Developers group.
 To post to this group, send email to android-developers@googlegroups.com
 To unsubscribe from this group, send email to
 android-developers+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/android-developers?hl=en




-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android Training in London: http://bit.ly/smand1 and http://bit.ly/smand2

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en