按下后退按钮时如何退出? [英] How to exit when back button is pressed?

查看:101
本文介绍了按下后退按钮时如何退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户按下意图上的后退"按钮时,应用程序应退出.如何确保按下后退按钮后退出应用程序?

When a user presses the back button on an intent, the application should quit. How can I ensure the application quits when the back button is pressed?

推荐答案

在使用startActivity开始新活动后,请立即调用finish(),以使当前活动不堆叠在新活动之后.

Immediately after you start a new activity, using startActivity, make sure you call finish() so that the current activity is not stacked behind the new one.

编辑 关于您的评论:

您所建议的并不是Android应用程序流通常如何工作以及用户期望它如何工作.您可以真正要做的是确保导致该活动的每个startActivity是一个startActivityForResult,并且具有一个onActivityResult侦听器,用于检查退出代码,然后再冒泡.您可以在此处详细了解.基本上,在完成一项活动之前使用setResult来设置您选择的退出代码,如果您的父项活动收到了该退出代码,则可以在 that 活动中进行设置,然后完成该任务,依此类推. ...

What you're suggesting is not particularly how the android app flow usually works, and how the users expect it to work. What you can do if you really want to, is to make sure that every startActivity leading up to that activity, is a startActivityForResult and has an onActivityResult listener that checks for an exit code, and bubbles that back. You can read more about that here. Basically, use setResult before finishing an activity, to set an exit code of your choice, and if your parent activity receives that exit code, you set it in that activity, and finish that one, etc...

这篇关于按下后退按钮时如何退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆