Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call an activity from service with Notification Bar

Tags:

android

This is my sistuation

A1 = Splash Screen Activity

A2 = Main Activity

A3 = Extra Activity

S1 = GPS Service

I start with A1 that creates intent to launch A2 and then A1 finish. Inside A2 I create and bind S1 (Inside S1 I make a Notification)

CharSequence text = getText(R.string.local_service_started);

Notification notification = new Notification(R.drawable.notify_icon, text, System.currentTimeMillis());

Intent i = new Intent();
i.setClassName("xxx.yyy.zzz.kkk", "xxx.yyy.zzz.kkk.A2");
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

notification.setLatestEventInfo(this, getText(R.string.local_service_label), text, contentIntent);
mNM.notify(NOTIFICATION, notification);

Now I have my icon in the notification bar If I press HOME button inside my A2 activity and open another application and then I press my notification icon all is working correctly and I gate back my A2 activity(A2 is the top most activity), but if inside A2 I launch A3 and go back HOME and press the notification I have the problems, A2 is created as a new instance(A2 now isn't top most)! Is possible to have the effect like long press HOME and focus the last open activity inside my application? I don't want to open a specific activity, but bring to front my paused activity without a new instance of the activity.

like image 609
GMG Avatar asked Oct 11 '25 21:10

GMG


1 Answers

I have solved, this open my application preserving the actual state and get the opened activity back form background to foregraund. Do the same think of app selecting from RECENTS !

Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
i.setComponent(new ComponentName(getApplicationContext().getPackageName(), MyAppClass.class.getName()));

MyAppClass is the 1st Activity your APK is launching.

Best regards

like image 154
GMG Avatar answered Oct 14 '25 14:10

GMG