java Android:什么时候用finish()结束课程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12912130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:46:00  来源:igfitidea点击:

Android: When to end class with finish()?

javaandroidclassactivity-finish

提问by Xander

I often see examples of classes which end with finish(), but definitely not always. My question is when should you end a class with finish()? And what does it do exactly, what is the difference between ending a class with the back button and ending it with finish()?

我经常看到以 结尾的类的例子finish(),但并非总是如此。我的问题是你应该什么时候结束课程finish()?它究竟做了什么,用后退按钮结束一个类和用它结束有什么区别finish()

Thanks in advance!

提前致谢!

回答by Rolf ツ

finish()can be called to kill (destroy) an Activity instance. If you don't need to close your Activity manual, which is true in many cases, you don't need to call this method.

finish()可以调用来杀死(销毁)一个 Activity 实例。如果你不需要关闭你的 Activity 手册,这在很多情况下是正确的,你不需要调用这个方法。

But if you require a button somewhere in your activity that says "close", then you should use this method. But in general the back button behavior in Android will handle things like this.

但是,如果您需要在活动中某处显示“关闭”的按钮,那么您应该使用此方法。但总的来说,Android 中的后退按钮行为会处理这样的事情。

The back button does not actually finish your activity, finish()calls the onDestory()method right away, while the back button does not.

后退按钮实际上并没有完成您的活动,它会立即finish()调用该onDestory()方法,而后退按钮则不会。

When the back button is pressed, the onStop()method is called, but the onDestory()method call might be delayed by the system, this so that the Activity can be resumed by the system which is cheaper (in resources) than a full restart.

当按下后退按钮时,该 onStop()方法被调用,但onDestory()方法调用可能会被系统延迟,这使得系统可以恢复 Activity,这比完全重启更便宜(在资源上)。

Lifecycle: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

生命周期:http: //developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Finish(): http://developer.android.com/reference/android/app/Activity.html#finish()

完成():http: //developer.android.com/reference/android/app/Activity.html#finish()

回答by Mohamed_AbdAllah

You finish Activityclass with finish()method when you need to end the Activityimmediatly at specific point. If you press back button, you will go through the lifecycle(onPause(), onStop()then onDestroy())automatically.

当您需要在特定点立即结束时,您可以Activity使用finish()方法完成课程Activity。如果您按下后退按钮,您将经历生命周期( onPause()onStop()然后onDestroy())自动。

回答by dorjeduck

One reason to use finish is when you have started the activity with

使用完成的原因之一是当您开始活动时

void android.app.Activity.startActivityForResult(Intent intent, int requestCode)

In this case you dont go back to the Activity which started it with another startActivity but with finish

在这种情况下,您不要返回使用另一个 startActivity 启动它的 Activity,但使用 Finish

回答by Renjith

public void finish ()

Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().

公共无效完成()

当您的活动完成并且应该关闭时调用它。ActivityResult 通过 onActivityResult() 传播回启动您的任何人。

finish()is called to kill your activity instance after you did what you have to do with the activity. After that, you have no use with the activity. So simply call finish()and it will kill your activity.

finish()在您完成与活动有关的操作后,会调用它来终止您的活动实例。在那之后,您将不再使用该活动。因此,只需拨打电话finish(),它就会终止您的活动。

If you press back button, it will call finish()automatically to kill the activity. It will go through the activity lifecycle(onPause(), onStop()and finally onDestroy())

如果您按返回按钮,它会finish()自动调用以终止活动。它将经历活动生命周期onPause()onStop()最后onDestroy()

The one reason with calling finish() is that, if you don't call it, when you navigate through activities, all the activities will be added to the backstack. So when you go back, you have to come back through these activities. So for e.g when you click back button, it simply kills the activity and it will be removed from the backstack.

调用 finish() 的一个原因是,如果你不调用它,当你浏览活动时,所有的活动都将被添加到后台堆栈中。所以当你回去的时候,你必须通过这些活动回来。因此,例如,当您单击后退按钮时,它只会终止活动,并将其从后台堆栈中删除。

回答by ThePCWizard

Back button takes you to the last activity in the stack whereas finish()destroys the current activity. You can call finish()on an activity for which you don't want the user return to. Example: LoginActivity

后退按钮会将您带到堆栈中的最后一个活动,而finish()销毁当前活动。您可以调用finish()不希望用户返回的活动。示例:登录活动