Activity finish() 中的问题以及在 Android 中按后退按钮的问题
我对使用 finish() 进行活动和使用键盘的后退按钮感到非常困惑。我创建了许多活动,因此从一个活动移动到另一个活动时我完成了最后一个活动。而且我在活动中使用了后退按钮,这样我就可以返回(从我来的地方) 通过按后退按钮,但当我使用键盘的后退按钮进入任何其他活动时出现问题。我希望它应该作为我的活动的后退按钮。
我认为这可以通过使用 onKeyListener 和检查键盘的后退按钮来完成,然后我开始与活动的后退按钮相同的活动。 我想知道这样做是否正确或者有比这更好的方法吗?
请帮忙
I am very confused in using finish() for activity and using back button of keypad.I have created many activities so on moving from one activity to another I finish the last activity. And also I have used back button in my activity so I can go back (from where I came)
by pressing back button but problem arises when I use keypad's back button I go to any other activity .I want it should work as my activity's back button.
And I think this can be done by using onKeyListener and chekcing keypad's back buttons and there I start activity same as my activity's back button.
I want to know is it right to do this way or there is a way better than this?
Please Help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一种更好的方法:
从一个活动转到另一个活动时,不应调用
finish()
。如果需要内存,操作系统会为您清除它,当您按回时,它将根据需要运行。只要确保你的生命周期函数是正确的(当你失去焦点等时,你会清理大块占用内存的东西),你就会成为一个更快乐的编码员。There is a better way:
You should not call
finish()
when going from one activity to another. If the memory is needed, the OS will clear it for you, and when you press back it will work as needed. Just make sure your lifecycle functions are correct (you clean up big chunks of memory hogging stuff when you loose focus etc), and you'll be a much happier coder .您可以重写 Activity 的
onBackPressed
方法来重写默认行为(即为 Activity 调用finish()
)。然而,当您日常执行此操作时,您应该质疑您的整体设计。请查看有关任务和返回堆栈的文档了解与 Android 的活动生命周期配合得更好的控制用户体验的其他方法。
You can override the activity's
onBackPressed
method to override the default behavior (which is to callfinish()
for your activity).You should question your overall design when you are doing this on a routine basis, however. Please review the documentation on Tasks and Back Stack for other methods of controlling the user experience that work better with Android's activity lifecycle.
尽量不要使用finish(),如果您想释放内存,那么不用担心操作系统会这样做,以防需要内存并且返回按钮可以使用。
我希望您了解 Activity 的生命周期。每当您按下后退按钮时,其他活动都会停止
Try not to use finish(), iF YOU WANT TO FREE MEMORY THEN DON'T WORRY ABOUT THAT os WILL DO THAT IN CASE MEMORY IS NEEDED AND BACK BUTTON WILL WORK TO.
I hope you know the life cycle of activity. Whenever you press the back button the other activity stops
如果您想调用
finish()
并希望设备后退按钮用作活动后退按钮,请重写onBackPressed()
方法。假设您在 Activity 2 中并且想要返回 Activity 1(这是您的第一个 Activity),然后在 Activity2.java 中编写以下代码...
If you want to call
finish()
and also want your device back button work as activity back button then override theonBackPressed()
method.Suppose you are in Activity 2 and and want to back in Activity 1 which is your first actvity then inside Activity2.java write below code ....