Cómo llevar a Android la actividad existente a través de la notificación

Tengo una aplicación de Android, cuando se ejecuta un servicio, quiero mostrar la notificación en la barra de estado. Entonces el usuario puede navegar a otra aplicación presionando la tecla HOME. Sin embargo, cuando intento llevar la aplicación anterior a la parte anterior mediante el icono de notificación, hay algún problema con la actividad existente. Incluso lo declaro como "Single Top" modo (quiero ejecutar la actividad existente ya que hay un servicio asociado en ejecución), de alguna manera que OnDestroy de la actividad se ha llamado antes de OnResume. Aquí está mi código de creación del objeto de notificación. ¿Podría por favor señalarme qué mal es. Gracias.

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); .... } 

 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); ... } 

Te recomiendo que lo hagas,

 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); .... } 

La DummyActivity debería ser simplemente una actividad que siempre termina en el evento oncreate.

En el archivo de manifiesto, agregue estas líneas

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

Espero que esto ayude …

Otra forma de lanzar la intención del paquete.

 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()); } 

Cuando una actividad está en segundo plano, Android puede destruir la actividad en cualquier momento para liberar recursos. Consulte la documentación de la actividad para obtener detalles completos sobre el ciclo de vida.

Su actividad debe estar preparada para guardar su estado en los métodos onPause o onSaveInstanceState. El documento mencionado anteriormente tiene detalles adicionales sobre cómo guardar el estado persistente.

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.