简体   繁体   English

详细的android活动生命周期(onAttachedToWindow())

[英]Detailed android activity lifecycle (onAttachedToWindow())

I'm interested in android activity lifecycle and I would like to get more detailed description/documentation/reference than widely available basic (onCreate->onStart->onResume) one . 我感兴趣的是Android的活动周期,我想获得比广泛使用的基本(onCreate-> onStart->的onResume)更详细的说明/文档/参考一个

My need comes from realizing that starting new activity ( Theme.Dialog styled) from onAttachedToWindow() greatly improves response time if comparing to starting it from onCreate(). 我的需要来自认识到,与从onCreate()启动相比,从onAttachedToWindow()启动新活动( Theme.Dialog风格的Theme.Dialog风格)可以大大缩短响应时间。 I wonder how this onAttachedToWindow() fits into whole android activity lifecycle. 我不知道此onAttachedToWindow()如何适合整个android活动生命周期。 Official API ref description "Called when the window has been attached to the window manager" doesn't help a lot. 官方API参考说明“在将窗口连接到窗口管理器时调用”没有太大帮助。

我猜想为什么这样会感觉更灵敏:我想如果您从活动A的onCreate()启动活动B,则活动A不会在活动B开始之前绘制,这可能需要一两秒钟(使得应用程序的响应速度较慢),如果您在活动A的onAttachedToWindow()中启动活动B,则启动A并在开始B之前就将其渲染,因此用户不必在空白屏幕上坐一秒钟或在看到对其动作的反应之前进行A活动。

  1. When the activity is launched, the onCreate() method is called. 启动活动时,将调用onCreate()方法。 In the onCreate() method you perform basic startup logic that should happen only once for the entire life of the activity. onCreate()方法中,您将执行基本的启动逻辑,该逻辑在活动的整个生命周期中仅应发生一次。

  2. After onCreate() is executed, the onStart() method is called. 执行onCreate()之后,将onStart()方法。 This method makes the activity visible as the app prepares the activity to enter the foreground and become interactive. 当应用程序准备活动进入前台并变得互动时,此方法使活动可见

  3. After onStart() is executed, the onResume() method is called. 执行onStart()后,将调用onResume()方法。 This method is called only when the app is in the foreground because this is the state in which the app interacts with the user. 仅当应用程序在前台时才调用此方法,因为这是应用程序与用户交互的状态。 The app stays in the resumed (or running) state until something happens to take focus away from the app. 该应用程序保持在恢复(或运行)状态,直到发生某些事情将焦点从该应用程序移开。

  4. If another activity comes into the foreground , the onPause() method is called. 如果另一个活动进入前台 ,则调用onPause()方法。 The app is paused if it is still visible . 如果该应用仍然可见,则会暂停。 This method only pauses the operations of the activity. 此方法仅暂停活动的操作。 It does not destroy. 它不会破坏。 The activity remains in the paused state until either the activity resumes or becomes completely invisible to the user. 该活动将保持在暂停状态,直到该活动恢复或对用户完全不可见为止。 4a . 4a If the user returns to the activity, the onResume() method is called again. 如果用户返回到活动,则再次调用onResume()方法。 4b . 4b the app process can be killed from the paused state if apps with higher priority need memory. 如果优先级较高的应用程序需要内存,则可以从暂停状态中终止应用程序进程。 If the user needs to return to app after it is killed, the onCreate() method is called again 如果用户被杀死后需要返回应用程序,则再次调用onCreate()方法

  5. If the activity is no longer visible to the user, the onStop() method is called. 如果该活动不再对用户可见 ,则onStop()方法。 When the activity is stopped, the activity object is kept in memory and maintains all state and information but is not attached to the window manager. 当活动停止时,活动对象将保留在内存中并维护所有状态和信息,但未附加到窗口管理器。 5a . 5a if the user returns to the activity, the onRestart() method is called, followed by the onStart() method again. 如果用户返回到活动,则将onRestart()方法,然后再次onStart()方法。 5b . 5b the app process can be killed from the stopped state if apps with higher priority need memory. 如果具有更高优先级的应用程序需要内存,则可以从停止状态终止应用程序进程。 If the user needs to return to the app after it is killed, the onCreate() method is called again. 如果用户被杀死后需要返回到该应用程序,则再次调用onCreate()方法。

  6. If the activity is finishing or being killed by the system, the onDestroy() method is called. 如果活动正在结束或被系统onDestroy()则调用onDestroy()方法。 The app is not initially shut down. 该应用程序最初并未关闭。 The system either calls this method because the activity is finishing due to someone's calling finish() , or because the system is temporarily destroying the process containing the activity to save space. 系统调用此方法是因为活动由于某人的调用finish() ,或者因为系统临时销毁了包含活动的进程以节省空间。 The system may also call this method when an orientation change occurs, and then immediately call onCreate() to recreate the process (and the components that it contains) in the new orientation. 当方向发生变化时,系统也可以调用此方法,然后立即调用onCreate()以新的方向重新创建流程(及其包含的组件)。 The onDestroy() method releases all resources that have not yet been released by earlier methods such as onStop() . onDestroy()方法释放所有尚未由早期方法(例如onStop() onDestroy()释放的资源。

The entire lifetime of an activity happens between the first call to onCreate() through a single final call to onDestroy() . 活动的整个生命周期发生在第一次调用onCreate()到最后一次调用onDestroy()

The visible lifetime of an activity happens between a call to onStart() until a call to onStop() . 活动的可见生命周期发生在调用onStart()到调用onStop()

The foreground lifetime of an activity happens between a call to onResume() until a call to onPause() . 活动的前台生存期发生在对onResume()的调用到对onPause()的调用之间。

The only method we must implement is onCreate() . 我们必须实现的唯一方法是onCreate() The others are called automatically. 其他的会自动被调用。 But we can implement them ourselves to tell the app what to do during those processes. 但是我们可以自己实现它们,以告诉应用在这些过程中该怎么做。

https://developer.android.com/guide/components/activities/activity-lifecycle.html#asem https://developer.android.com/guide/components/activities/activity-lifecycle.html#asem

onAttachedToWindow is called when the view is attached to a window. 当视图附加到窗口时,将调用onAttachedToWindow At this point it has a Surface and will start drawing. 此时,它具有一个Surface并将开始绘制。

https://developer.android.com/reference/android/view/View.html#onAttachedToWindow() https://developer.android.com/reference/android/view/View.html#onAttachedToWindow()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM