How to transfer existing Android activities to the front through notification

I have one Android application, when it starts the service, I want to show a notification in the status bar. Then the user can switch to another application by pressing the HOME key. However, when I try to return the previous running application to the Front through the notification icon, there are some problems with the existing activity. Even I declare it as a "Single Top" mode (I want to start an existing activity, since there is work associated with it), anyway, that the OnDestroy activity was called before OnResume. Here is my notification object creation code. Could you please indicate that this is not so. Thank you

private void showNotification () { Intent toLaunch = new Intent(getApplicationContext(), MySingleTopActivity.class); PendingIntent intentBack = PendingIntent.getActivity(getApplicationContext(), 0,toLaunch, PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(getApplicationContext(), getText(R.string.GPS_service_name), text, intentBack); .... } 
+13
android
Jul 28 '10 at 18:20
source share
4 answers
 private void showNotification() { Intent toLaunch = new Intent(getApplicationContext(), MySingleTopActivity.class); //add these two lines will solve your issue toLaunch.setAction("android.intent.action.MAIN"); toLaunch.addCategory("android.intent.category.LAUNCHER"); PendingIntent intentBack = PendingIntent.getActivity(getApplicationContext(), 0, toLaunch, PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(getApplicationContext(), getText(R.string.GPS_service_name), text, intentBack); ... } 
+23
May 31 '11 at 12:41
source share

I would recommend you do this,

 private void showNotification () { Intent toLaunch = new Intent(getApplicationContext(), DummyActivity.class); // You'd need this line only if you had shown the notification from a Service toLaunch.setAction("android.intent.action.MAIN"); PendingIntent intentBack = PendingIntent.getActivity(getApplicationContext(), 0,toLaunch, PendingIntent.FLAG_UPDATE_CURRENT); .... } 

DummyActivity should simply be an activity that always ends in an oncreate event.

In the manifest file add these lines

 <activity class=".DummyActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> </intent-filter> </activity> 

Hope this helps ...

+8
Jul 20 2018-11-21T00:
source share

Another way to run your package.

 private void NotificationwithLaucherSelfPackage(Context context , int notification_id){ Notification noti = new Notification.Builder(context) .setContentTitle("Your Title") .setContentText("Your Text") .setSmallIcon(R.drawable.abc_ic_menu_share_mtrl_alpha) .setContentIntent(PendingIntent.getActivity( context ,notification_id , getLauncherIntent(context) , PendingIntent.FLAG_UPDATE_CURRENT)) .build(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); } private Intent getLauncherIntent(Context context){ return context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()); } 
0
06 Oct '15 at 9:44
source share

When an action is in the background, Android can kill the action at any time to free up resources. See the Activity documentation for detailed lifecycle information.

Your activity should be ready to save its state in the onPause or onSaveInstanceState methods. The above document provides more information about maintaining a steady state.

-2
Jul 28 '10 at 18:27
source share



All Articles