0

I create a notification in my application. When I have multiple notifications, I need to press on with each opening Activity and perform actions depending on the parameters of the transmitted with intent. for this I set in intent flags

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

but then if you click on the notification at the time when the application is open or closed then everything is working correctly. But if the application is minimized, the error output.

I changed the flag

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

Now when the application is minimized, and I click on the notification it opens normally. But there are other problems. Fragments are opened, and the data is loaded.

That's how I generate notifications:

private void generateNotification(Context context, String title, String message,int groupid,Intent data) {

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    Intent intent = new Intent(context,MyActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

    if (groupid == 1) {
        intent.putExtra("guest",data.getStringExtra("guest"));
        intent.putExtra("hotel",data.getStringExtra("hotel"));
        intent.putExtra("room",data.getStringExtra("room"));
    }

    if (groupid==4){
        intent.putExtra("hotel",data.getStringExtra("hotel"));
        intent.putExtra("guest",data.getStringExtra("guest"));
    }

    if (groupid==5){
        intent.putExtra("hotel",data.getStringExtra("hotel"));
    }

    int requestCode = ("requestCode" + System.currentTimeMillis()).hashCode();
    intent.putExtra("group_id",groupid);
    intent.setAction(String.valueOf(requestCode));
    Log.d("mylogout", "group_id: " + groupid);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification notification   = new NotificationCompat.Builder(context)
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.drawable.ic_stat_gcm)
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))
                    .setTicker("Новое сообщение")
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(true)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .build();
    notificationManager.notify(requestCode, notification);
}

That is how I get the intent to Activity:

@Override
public void onNewIntent(Intent intent) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    Bundle arguments;
    Fragment fragment;
    String guest;
    Bundle extras = intent.getExtras();
    if (extras != null) {
        if (extras.containsKey("group_id")) {
            int msg = extras.getInt("group_id");
            String hotel = extras.getString("hotel");
            switch (msg) {
                case 5:
                    fragment = new PagerLogbookContainer();
                    arguments = new Bundle();
                    arguments.putString(ARGUMENT_PAGE_HOTEL, hotel);
                    fragment.setArguments(arguments);
                    fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
                    setTitle(hotel);
                    break;
                case 1:
                    guest = extras.getString("guest");
                    String room = extras.getString("room");
                    fragment = new Guest();
                    arguments = new Bundle();
                    arguments.putString("guestid", guest);
                    arguments.putString("hotel", hotel);
                    arguments.putString("room", room);
                    fragment.setArguments(arguments);
                    fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    fragmentManager.beginTransaction().replace(R.id.container, fragment, "guest").commit();
                    break;
                case 4:
                    guest = extras.getString("guest");
                    fragment = new Survey();
                    arguments = new Bundle();
                    arguments.putString("guestid", guest);
                    arguments.putString("hotel", hotel);
                    fragment.setArguments(arguments);
                    fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
                    break;
            }
        }
    } else {

        fragment = new StatusInfo();
        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
        setTitle("Статус инфо");

    }
}

and calls this method in onCreate() in MyActivity.

I want to be notified when pressed, opens my Activity and sets the necessary fragment if Activity is in any state.

if the flag is set intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); and the application is minimized error output:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
            at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1365)
            at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1383)
            at android.support.v4.app.FragmentManagerImpl.popBackStack(FragmentManager.java:507)
            at com.managment.pavel.managmentgradle.MyActivity.onNewIntent(MyActivity.java:158)
            at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1123)
            at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2042)
            at android.app.ActivityThread.performNewIntents(ActivityThread.java:2055)
            at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2064)
            at android.app.ActivityThread.access$1400(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1194)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4464)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:589)
            at dalvik.system.NativeStart.main(Native Method)
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
Valera Valerianov
  • 247
  • 1
  • 3
  • 14

1 Answers1

0

Check this answer IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager

it looks like you have problem with your fragmentManager

Community
  • 1
  • 1
Vika L.
  • 265
  • 1
  • 11