简体   繁体   English

如果最小化应用程序,则在单击通知时应用程序崩溃

[英]Application crashes when clicking on a notification if the application is minimized

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. 并在MyActivity的onCreate()中调用此方法。

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); 如果标志设置为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)

Check this answer IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager 检查此答案IllegalStateException:使用ViewPager在onSaveInstanceState之后无法执行此操作

it looks like you have problem with your fragmentManager 看来您的fragmentManager有问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM