2016-06-29 1 views
1

J'ai une activité avec quelques édits et un En cours de notification. Après avoir rempli TextTexts, je reviens à l'écran d'accueil en appuyant sur le bouton Accueil (mon application fonctionne en arrière-plan). Tout ce que je veux, c'est revenir à mon activité avec EditTexts rempli (ne pas en créer un nouveau) lorsque je clique sur la notification en cours.Revenir à l'activité précédente lorsque la notification en cours est cliquée

J'ai essayé

How should i do from notification back to activity without new intent

Et ce

Notification click: activity already open

Ils ne fonctionnent pas du tout !!!

Ci-dessous mon extrait de code

ProtocolMonitorActivity.java

Intent resultIntent = new Intent(this, ProtocolMonitorActivity.class); 

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
    stackBuilder.addParentStack(ProtocolMonitorActivity.class); 
    stackBuilder.addNextIntent(resultIntent); 

    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(
      this).setSmallIcon(R.drawable.noti_icon) 
      .setContentTitle("Protocol Monitor App") 
      .setContentText("Service is running") 
      .setOngoing(true); 
    notiBuilder.setContentIntent(resultPendingIntent); 

    notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notiManager.notify(notiId, notiBuilder.build()); 

Manifest

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

    <activity 
     android:name=".ProtocolMonitorActivity" 
     android:label="@string/app_name" 
     android:process="com.android.phone" 
     android:launchMode="singleTop" 
     android:screenOrientation="portrait" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
      <category android:name="android.intent.category.DEVELOPMENT_PREFERENCE" /> 
     </intent-filter> 

     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value=".ProtocolMonitorActivity" /> 

    </activity> 
</application> 

ce que quelqu'un a une idée à ce sujet?

Merci beaucoup!

Répondre

2

Vous ne pouvez pas le faire en utilisant TaskStackBuilder. Le comportement de TaskStackBuilder est qu'il efface toujours la tâche (recréer toutes les activités).

Vous avez juste besoin d'un « lancement intention » d'apporter votre tâche au premier plan dans l'état où il se trouve être dans Il y a 2 façons de le faire:.

Varient 1:

final Intent notificationIntent = new Intent(this, ProtocolMonitorActivity.class); 
notificationIntent.setAction(Intent.ACTION_MAIN); 
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

Variant 2:

final Intent notificationIntent = 
     PackageManager.getLaunchIntentForPackage(getPackageName()); 

Effectuez ensuite:

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, 
     notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(
     this).setSmallIcon(R.drawable.noti_icon) 
     .setContentTitle("Protocol Monitor App") 
     .setContentText("Service is running") 
     .setOngoing(true); 
notiBuilder.setContentIntent(resultPendingIntent); 

notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
notiManager.notify(notiId, notiBuilder.build()); 
+1

Ça a juste marché. Merci pour l'explication de détail !!! –

+0

@ Viet-AnhDinh Salut, le même problème me trouble. Cette méthode fonctionne uniquement lorsque vous fermez votre application. Mais lorsque l'activité est déjà ouverte, elle redémarre. Comment le résoudre sans le mode SingleTask? – AnswerZhao

+0

@AnswerZhao ce n'est pas vrai. Cette solution ramène simplement la tâche existante au premier plan, quel que soit l'état dans lequel elle se trouvait. Elle ne redémarre plus rien. Si vous avez un problème spécifique, vous devriez poster une nouvelle question au lieu de faire un commentaire sur une réponse existante. Cela vous attirera plus d'attention. –

0

Il suffit d'insérer cette ligne

resultIntent.setAction(Intent.ACTION_MAIN); 
+0

Salut. Je vous remercie. J'ai essayé et pas de chance. –

+0

Jetez un coup d'oeil à ceci; http://stackoverflow.com/a/21424959/5291413 – Berkay92

+0

Merci! Je dois d'abord supprimer TaskStackBuilder. –