android - 我的 android 应用程序在 onclick 时没有关闭

标签 android kill-process

我需要在单击退出按钮时关闭我的应用程序,尝试过许多来自谷歌的解决方案,如抽象类、完成、终止进程等,但它从未给出任何解决方案

` Screen 1 --> Screen2 --> Screen3 --> Screen 1 ---> screen 2 --> Screen 1 --> 需要关闭应用程序

最佳答案

没有直接的方法可以做到这一点,我就是这样做的希望这对你有帮助。

试试这个对我来说效果很好

// clear whole activity stack

    Intent intent = new Intent("clearStackActivity");
    intent.setType("text/plain");
    sendBroadcast(intent);

// start your new activity
Intent intent = new Intent(OrderComplete.this,
                    MainActivity.class);
startActivity(intent);

步骤:1

将这些行放在所有 Activity 的 onCreate() 方法中,或者如果您有任何基本 Activity 可以将其放在那里,则无需放入所有 Activity。

private KillReceiver clearActivityStack;
clearActivityStack = new KillReceiver();
        registerReceiver(clearActivityStack, IntentFilter.create("clearStackActivity", "text/plain"));

步骤:2

把这个类放在你的 Base activity 中

private final class KillReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    }

解释:

在上面的代码中,我们创建了自定义广播接收器。我们将其注册到基本 Activity 中,即在所有调用的 Activity 中。

当我们想要完成所有 Activity 时,我们只是广播 Intent ,因此所有注册此接收器的 Activity 都会通知并自行完成。

关于android - 我的 android 应用程序在 onclick 时没有关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23515161/

相关文章:

android - react-native-sound,循环之间的间隙(Android)

android - 我如何使用(DUN 或 PAN)从 android 连接到另一部手机?

python - subprocess:删除Windows中的子进程

android - 一键式安卓应用 killer

java - 如何在获取最后位置时解决 mLastLocation、mLatitudeText、mLongitudeText

android - 更改配置时 ListView 项目未正确刷新(旋转手机)

android - 文本不会实时更改

powershell - 尝试通过 PowerShell 脚本查找并终止进程

php - 将 PHP 进程作为守护进程运行,同时从后台安全地终止它