Java Activity.finish() 在 Android 中是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2590947/
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-08-13 09:34:04  来源:igfitidea点击:

How does Activity.finish() work in Android?

javaandroid

提问by rantravee

Could someone provide a description of what happens when an Activitycalls its finish()method?

有人可以描述Activity调用其finish()方法时会发生什么吗?

Does it exit immediately, or does it complete the function from which it was called?

它是立即退出,还是完成调用它的函数?

采纳答案by CommonsWare

Does it exits immediately or completes the function from which it was called ?

它是立即退出还是完成调用它的函数?

The method that called finish()will run to completion. The finish()operation will not even begin until you return control to Android.

调用的方法finish()将运行完成。在finish()您将控制权返回给 Android 之前,该操作甚至不会开始。

回答by user284295

ondestroy()is the final call you receive before your activity is destroyed.

ondestroy()是在您的活动被销毁之前收到的最后一个电话。

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.

这可能是因为 Activity 正在完成(有人调用finish()它,或者因为系统正在临时销毁该 Activity 实例以节省空间)。您可以使用该isFinishing()方法区分这两种情况。

回答by Darpan

If there are two activities A and B. And your flow is going from A > B; and B=A calls finish().

如果有两个活动 A 和 B。你的流程是从 A > B; 并且 B=A 调用finish()

Then,

然后,

The method where you called finish()from will execute as Mark mentioned. And flow of callbacks will be as followed -

您调用的方法finish()将按照 Mark 提到的方式执行。回调流程如下 -

  1. onPause()of activity A
  2. onRestart()> onStart()> onResume()of Activity B
  3. Then, comes the real difference. If you did not call finish()from activity A; only onStop()of Activity Awill be called here. While, in this case, where we called finish()from Activity A; So onStop()and onDestroy()both will be called for activity A.
  1. onPause()activity A
  2. onRestart()> onStart()>onResume()活动B的
  3. 然后,真正的区别来了。如果您没有finish()activity A; 只有onStop()ofActivity A会在这里被调用。而在这种情况下,我们finish()从哪里调用Activity A;因此onStop()onDestroy()两者都将被调用activity A

回答by user3688593

Every life cycle event like onCreate, onResume, onPause.... onDestroyof an Activity is always called on a single thread - The "Main thread".

Activity 的每个生命周期事件,如onCreate, onResume, onPause....onDestroy总是在单个线程上调用 - “主线程”。

In short this thread is backed by a Queue into which all the activity events are getting posted. This thread can execute all these events in the order of insertion.

简而言之,这个线程由一个队列支持,所有活动事件都发布到该队列中。这个线程可以按照插入的顺序执行所有这些事件。

If you are calling finish()in one of the life cycle callbacks like onCreate()...a "finish" message will get added to this queue but the thread is not free to pick & execute "finish" action until currently executing method returns i.e Thread is freed from current task.

如果您正在调用finish()生命周期回调之一,例如onCreate()......“完成”消息将被添加到此队列中,但线程不能自由选择并执行“完成”操作,直到当前执行的方法返回即线程被释放当前任务。