如何使用户离开当前活动时,一些code执行(pressing回硬件按钮) [英] How to make some code execute when the user leaves the current activity (Pressing back hardware button)

查看:84
本文介绍了如何使用户离开当前活动时,一些code执行(pressing回硬件按钮)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为标题所说,我该怎么办呢?我看着在互联网上,似乎你不能做..任何人都知道如何才能做到呢?

As title says, how can I do it? I looked over the internet and it seems that you just can't do it.. Anyone knows how can it be done?

我的意思是,我知道如何执行我的code,当用户presses一个按钮,关闭活动,但对于硬件后退按钮?

I mean, I know how to execute my code when the user presses a button that closes the activity, but what about the hardware back button?

感谢您!

推荐答案

覆盖 onBack pressed()

@Override
public void onBackPressed() {
    //code
}

当使用后退按钮这将执行。如果你想,即使用户导航到另一个活动,或使用Home键来运行code,覆盖的onPause()

这篇关于如何使用户离开当前活动时,一些code执行(pressing回硬件按钮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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