在通过“后退”按钮离开应用程序之前显示“是/否”对话框

发布于 2024-09-29 19:06:59 字数 139 浏览 3 评论 0原文

如果用户反复按后退按钮,我需要一种方法来检测他们何时处于任务/应用程序的最后一个活动并显示“您想退出吗?”他们返回主屏幕或之前运行的任何应用程序之前的对话框。

挂钩 onkeypressed() 很容易,但是我如何确定这是任务中的“最后一个”活动?

If user repeatedly presses back button, I need a way to detect when they are on the very last activity of my task/app and show "Do you want to exit?" dialog befor they return to Home Screen or whatever previous app they had running.

Its easy enough to hook onkeypressed(), but how do I figure out that this is a "last" activity in the task?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

[旋木] 2024-10-06 19:06:59

我认为你可以在你的 Activity 中使用这样的方法来检查它是否是最后一个:

private boolean isLastActivity() {
    final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    final List<RunningTaskInfo> tasksInfo = am.getRunningTasks(1024);

    final String ourAppPackageName = getPackageName();
    RunningTaskInfo taskInfo;
    final int size = tasksInfo.size();
    for (int i = 0; i < size; i++) {
        taskInfo = tasksInfo.get(i);
        if (ourAppPackageName.equals(taskInfo.baseActivity.getPackageName())) {
            return taskInfo.numActivities == 1;
        }
    }

    return false;
}

这还需要向你的 AndroidManifest.xml 添加权限:

<uses-permission android:name="android.permission.GET_TASKS" />

因此在你的 Activty 中你可以使用以下内容:

public void onBackPressed() {
    if (isLastActivity()) {
         showDialog(DIALOG_EXIT_CONFIRMATION_ID);
    } else {
         super.onBackPressed(); // this will actually finish the Activity
    }
}

然后在你的对话框句柄中单击按钮调用 Activity.finish()。

I think you can use smth like this in your Activity to check if it is the last one:

private boolean isLastActivity() {
    final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    final List<RunningTaskInfo> tasksInfo = am.getRunningTasks(1024);

    final String ourAppPackageName = getPackageName();
    RunningTaskInfo taskInfo;
    final int size = tasksInfo.size();
    for (int i = 0; i < size; i++) {
        taskInfo = tasksInfo.get(i);
        if (ourAppPackageName.equals(taskInfo.baseActivity.getPackageName())) {
            return taskInfo.numActivities == 1;
        }
    }

    return false;
}

This will also require to add a permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.GET_TASKS" />

Thus in your Activty you can just use the following:

public void onBackPressed() {
    if (isLastActivity()) {
         showDialog(DIALOG_EXIT_CONFIRMATION_ID);
    } else {
         super.onBackPressed(); // this will actually finish the Activity
    }
}

Then in youd Dialog handle the button click to call Activity.finish().

花伊自在美 2024-10-06 19:06:59

请查看Android应用程序基础,这违反了Android应用程序的推广行为:

当用户按下BACK键时,屏幕不会显示用户刚刚离开的Activity(上一个Activity的根Activity)任务)。相反,堆栈顶部的活动将被删除,并显示同一任务中的前一个活动。

Please review the Android Application Fundamentals, this violates the promoted behavior of an Android Application:

When the user presses the BACK key, the screen does not display the activity the user just left (the root activity of the previous task). Rather, the activity on the top of the stack is removed and the previous activity in the same task is displayed.

一笑百媚生 2024-10-06 19:06:59

哎呀,没有。

您可以使用一些标志来影响历史堆栈,但它们都在您的应用程序中。在您的 Intent 中尝试使用此标志:

FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY

FLAG_ACTIVITY_NO_HISTORY

FLAG_ACTIVITY_REORDER_TO_FRONT

FLAG_ACTIVITY_SINGLE_TOP

Afaik, there is not.

There are a few flags you can use to influence the history stack, but all within your application. Try this flags with your Intent:

FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY

FLAG_ACTIVITY_NO_HISTORY

FLAG_ACTIVITY_REORDER_TO_FRONT

FLAG_ACTIVITY_SINGLE_TOP

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文