【问题标题】:intent starts again after finish完成后意图再次开始
【发布时间】:2021-02-23 12:14:22
【问题描述】:

我使用一个按钮退出我的应用程序,但是,当我退出它并按下最近的应用程序按钮时(下图) 并通过灰屏重新进入我的应用程序,而不是从主要活动启动应用程序,它从最近的活动开始并崩溃,因为它有 bo 数据.. 即使应用程序崩溃了,当我再次按下灰色活动时,它再次打开第二个活动并再次崩溃..

灰屏图片(https://ibb.co/TqPnXJR)

如何解决当用户点击灰屏时会启动主要活动的问题?

这是我的代码:

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Stop Navigation ");
        builder.setMessage("Are you sure you want to stop navigation?");
        builder.setCancelable(false);
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {


                exitButtonPressed = true;


                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

                //finishAffinity(); 
                //System.exit(0); 
                //finishAndRemoveTask(); 
                //android.os.Process.killProcess(android.os.Process.myPid());

// I TRIED ALL THE ABOVE BUT ITS ITS NOT WORKING.. 



            }
        });

谢谢!!

编辑:

当点击灰色方块时,它会激活 OnResume..

我注意到,每次我删除我的任务并通过我的前台服务进入它发生的第二个活动时。 我认为它与未决意图有关。

这是我的待处理意图代码:

Intent i = new Intent(getApplicationContext(), MapsActivity.class);
i.putExtra("AddressBackground22", mAddress);
i.putExtra("AddressType22", mAddressType);
i.putExtra("AddressLatBackground22", destinationLat);


PendingIntent activityPendingIntent = PendingIntent.getActivity(this, 25, i, 0);

    NotificationCompat.Action action2 = new NotificationCompat.Action(R.drawable.ic_cancel, getString(R.string.remove_location_updates), servicePendingIntent);

NotificationCompat.Action action = new NotificationCompat.Action(R.drawable.ic_launch, getString(R.string.launch_activity), activityPendingIntent);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
            .setContentTitle("App is running in background")
            .addAction(action)
            .addAction(action2)
            .setColor(getColor(R.color.foreGroundBackgroundColor))
            .setPriority(NotificationManager.IMPORTANCE_HIGH)
            .setCategory(Notification.CATEGORY_SERVICE)
            .setSmallIcon(R.drawable.applogo)
            .build();

【问题讨论】:

    标签: android android-intent exit activity-finish


    【解决方案1】:

    你为什么要使用

     Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
    

    只需调用

    finish();
    

    点击按钮

    【讨论】:

    • 是的,你是对的,但应该有 finishAffinity();未完成();
    • 嘿@Abhnav Maurya 感谢您的回复!如果我只使用完成();它向我显示了最近的应用程序菜单,而不是灰屏,我可以看到正在运行的活动..当我通过最近的应用程序进入我的应用程序时,它只是崩溃了..
    • @Mr.Goomer 可以发日志吗?运行应用程序。到达这个对话框。并重新打开应用程序。完成亲和力();将关闭所有正在运行的活动。问题可能出在其他活动中。例如 main 或 your splash。
    • 这是直到 onResume 开始的日志:pastebin.com/Lr2hCAhq 它不应该恢复,而是从第一个活动开始应用程序。谢谢
    • 删除该意图代码。当一些活动出现在前面并且用户返回应用程序时,onResume 就会运行。如果应用在后台(最小化)应用从 onCreate 开始。
    【解决方案2】:
    YourActivity.this.finish();
    

    您应该在完成活动之前关闭对话框

    Dialog.dismiss();
    

    希望可能会有所帮助。

    【讨论】:

    • 嘿@Usama Altaf 感谢重播!它仍然无法正常工作..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 2016-11-09
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多