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
Solution pour quoi faire? – kumar
fragmentTransaction.commit(); utilisez ensuite fragmentTransaction.commitAllowingStateLoss(); – jinkal
Ok, je vais essayer .. Merci .. – kumar