截获后退按钮按钮

2023-09-04 06:20:12 作者:信手拈清风

我在写这将有两个活动,当用户presses在第二个活动一个对话框会弹出要求用户确认操作后退按钮的应用程序。那么,如何拦截呢?我严重怀疑这个怎么把backstack是操作系统本身的一部分。有没有人找到了一个解决办法?

I'm writing an application which will have two Activities, when the user presses the back button on the second activity a dialog should pop up asking the user to confirm the action. So how do I intercept this? I seriously doubt about this coz the backstack is a part of the OS itself. Has anyone found a workaround?

推荐答案

在一个活动,你可以只覆盖

In an activity you can just override

onBack pressed()

onBackPressed()

编辑:这是API LVL 5+:/ 4和下面你得重写的onkeydown()

edit: that is api lvl 5+ :/ for 4 and below you gotta override onKeyDown()