且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我们可以在startActivity()之后执行代码,然后再暂停它吗?

更新时间:2023-09-30 13:43:58

要在调用startActivity()的方法中执行的任何操作都会在 接收到onPause()的调用之前被执行. .关键是,您的应用程序默认情况下使用一个主线程,对onPause()的调用以及发生在其上的其他生命周期方法.因此,尽管此线程忙于执行您方法中的代码,但无法处理其他任何事情.

Anyhting you want execute in the method with the call to startActivity() will get executed before you receive a call to onPause(). The thing is, your application by default uses one main thread, calls to onPause() and other lifecycle methods happen on it. So while this thread is busy with executing the code in your method, it can't process anything else.

仅当您的方法在其他线程中执行时,这才是问题.但是,情况并非如此,因为此方法用于侦听UI事件,所以我假定总是从主线程调用它.

This would only be a problem if your method were executed in some other thread. This is not the case, however, since this method is used to listen to UI events, so I assume that it is always called from the main thread.