1

Lorsque l'application est au premier plan et reçoit la notification, la transition du fragment sur l'activité se déroule sans problème. Si l'application s'exécute en arrière-plan, l'application se bloque lorsque la transition du fragment se déclenche avant que l'activité ne soit mise en avant-plan!Notification Android Touchez: Transition de fragment lorsque l'application n'est pas au premier plan

Code pour la création de notification:

Intent intent = new Intent(this, MainActivity.class); 
    if(newsData == true) { 
     intent.putExtra("displayNews", true); 
     } else { 
     intent.putExtra("displayMessage", true); 
     } 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); 

    final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
     .setSmallIcon(R.drawable.ic_launcher) 
     .setContentTitle(this.getApplicationContext().getString(R.string.app_name)) 
     .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg); 

    mBuilder.setContentIntent(contentIntent); 
    mBuilder.setAutoCancel(true); 
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 

MainActivity qui reçoit la notification:

@Override 
    protected void onNewIntent(final Intent intent) { 
     final Bundle extras = intent.getExtras(); 
     if (intent.getExtras().get("displayNews") != null) { 
      showFragment(context, "newsFragment", false); 
     } else { 
      showFragment(context, "messageFragment", false); 
     } 
    } 

    public void showFragment(final FragmentActivity context, final String selectedItem, final boolean reverseAnimation) { 

     final Fragment switchFragment = getFragment(selectedItem); 
     final FragmentManager fragmentManager = context.getSupportFragmentManager(); 
     final Fragment fragment = fragmentManager.findFragmentByTag(selectedItem); 
     if (fragment == null) { 
      final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
      if (!reverseAnimation) { 
      fragmentTransaction.setCustomAnimations(R.anim.abc_fade_in, R.anim.slide_out_left); 
      } else { 
      fragmentTransaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right); 
      } 
      fragmentTransaction.replace(R.id.content_frame, switchFragment, selectedItem); 
      fragmentTransaction.commit(); 
     } 
    } 

Comment attendre dans le MainActivity de l'activité à venir au premier plan avant de commencer la transition de fragment ?

journal d'erreur:

Application has been shut down. Please inspect exception trace for more details. 
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 

Répondre

1

à la fin de la bonne solution était (comme on le voit dans les commentaires) à utiliser.

fragmentTransaction.replace(R.id.content_frame, switchFragment, selectedItem); 
     fragmentTransaction.commitAllowingStateLoss(); 
+0

Solution pour quoi faire? – kumar

+0

fragmentTransaction.commit(); utilisez ensuite fragmentTransaction.commitAllowingStateLoss(); – jinkal

+0

Ok, je vais essayer .. Merci .. – kumar

1

Vous pouvez attendre jusqu'à ce que onStart() avant de faire la transition, puisque c'est lorsque l'interface utilisateur sera visible (et vous semblez faire des animations). Utilisez un booléen pour savoir si vous êtes ou non entre onStart() et onStop() - si vous êtes, faites la transition; sinon, stockez des informations sur la transition en attente et attendez de le faire dans onStart().

+0

Merci Karakuri ... va essayer. – kumar