这是我从官网找到的API解释,具体是在android.app.Activity包下

finish()

open fun finish(): Unit

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

onDestroy()

protected open fun onDestroy(): Unit

Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called finish on it), or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing method.

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause or onSaveInstanceState, not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

If you override this method you must call through to the superclass implementation.

看到一大串英文应该头大吧,我来解释下。onDestroy()被调用:这可能是因为活动正在完成,也可能是因为系统临时销毁活动的此实例以节省空间。并且可以使用isFinishing方法来区分这两种情况。而finish()是当您的活动完成并应关闭时,请调用此选项。

所以onDestroy是俩种情况。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐