简体   繁体   English

活动何时可见? 在 onStart() 或 onResume() 之后?

[英]When activity will be visible? After onStart() or onResume()?

Docs say:文档说:

The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop() .活动的可见生命周期发生在对onStart()的调用与对onStop()的相应调用之间。

Also in this one you can see:同样在这个你可以看到:

The onStart() call makes the activity visible to the user... onStart()调用使活动对用户可见...

So I thought the UI of Activity must be visible after returning from onStart() .所以我认为 Activity 的 UI 在从onStart()返回后必须是可见的。

Recently, for testing purpose, I put an infinite loop in onResume() and guessed that the UI of Activity must be visible.最近为了测试,在onResume()放了一个无限循环,猜想Activity的UI一定是可见的。 But the result of starting Activity was a white window without any UI.但是启动Activity的结果是没有任何UI的白色window。

So it seems that UI of Activity will be visible after returning from onResume() instead of returning from onStart() .因此,从onResume()返回而不是从onStart()返回后,似乎 Activity 的 UI 将可见。 Is this true?这是真的? If yes, why docs do say such misleading statements?如果是,为什么文档会说这样的误导性陈述?

Edit:编辑:

You may think like Công Hải which says :你可能会像 Công Hải 那样认为

I think the documents mention about window visible not view visible.我认为文件提到 window 可见而不是可见。

But I do not think that "visibility" means above mentioned "white window";但我不认为“可见性”是指上面提到的“白窗”; because if you put an infinite loop in onCreate() , result of starting Activity again will be a white window, while as docs say the onCreate() is not in "visible lifetime" of Activity.因为如果你在onCreate()中放置一个无限循环,再次启动 Activity 的结果将是一个白色的 window,而正如文档所说, onCreate()不在 Activity 的“可见生命周期”中。 So "visibility of Activity" must mean another thing than "white window".所以“Activity的可见性”一定是指“白窗”以外的东西。

Edit2编辑2

Alongside official docs, many tutorials say the Activity will be visible by calling onStart() and interactive by calling onResume() .除了官方文档,许多教程都说通过调用onStart()可以看到 Activity,通过调用onResume()可以交互。 Are they all say incorrect thing without testing?他们都是未经测试就说错话吗?

Take a closer look at the docs that you linked .仔细查看您链接的文档 It states:它指出:

If an activity has lost focus but is still presented to the user, it is visible.如果活动失去焦点但仍呈现给用户,则它是可见的。 It is possible if a new non-full-sized or transparent activity has focus on top of your activity, another activity has higher position in multi-window mode, or the activity itself is not focusable in current windowing mode.如果一个新的非全尺寸或透明活动的焦点位于您的活动之上,另一个活动在多窗口模式下具有更高的 position,或者活动本身在当前窗口模式下不可聚焦,则可能是这样。 Such activity is completely alive (it maintains all state and member information and remains attached to the window manager).这样的活动是完全活跃的(它维护所有 state 和成员信息,并保持连接到 window 管理器)。

If an activity is completely obscured by another activity, it is stopped or hidden.如果一个活动被另一个活动完全遮蔽,它就会停止或隐藏。 It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.它仍然保留所有 state 和成员信息,但是,它不再对用户可见,因此它的 window 被隐藏,并且当其他地方需要 memory 时,它通常会被系统杀死。

[...] [...]

The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop().活动的可见生命周期发生在对 onStart() 的调用与对 onStop() 的相应调用之间。 During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user.在此期间,用户可以在屏幕上看到活动,尽管它可能不在前台并与用户交互。 Between these two methods you can maintain resources that are needed to show the activity to the user.在这两种方法之间,您可以维护向用户显示活动所需的资源。 For example, you can register a BroadcastReceiver in onStart() to monitor for changes that impact your UI, and unregister it in onStop() when the user no longer sees what you are displaying.例如,您可以在 onStart() 中注册 BroadcastReceiver 以监视影响 UI 的更改,并在用户不再看到您正在显示的内容时在 onStop() 中取消注册。 The onStart() and onStop() methods can be called multiple times, as the activity becomes visible and hidden to the user. onStart() 和 onStop() 方法可以多次调用,因为活动对用户可见和隐藏。

This paints a pretty clear picture of what is meant by "visible": Your activity controls at least a part of what is shown on the screen.这清楚地描绘了“可见”的含义:您的活动至少控制了屏幕上显示的部分内容。

With blocking onResume you have prevented your activity from launching as normal and/or rendering your content but it is still visible to the user (the white screen) and obscuring a previously visible activity.通过阻止onResume ,您可以阻止您的活动正常启动和/或呈现您的内容,但它仍然对用户可见(白屏)并遮盖了以前可见的活动。

Putting an infinite loop in onResume() is not really going to tell you the whole story.onResume()中设置一个无限循环并不能真正告诉你整个故事。 There are a lot of activities that are queued to the main (UI) thread (like painting the screen) and since onResume() is called on the main (UI) thread you are preventing Android from doing any work on the main (UI) thread.有很多活动排队到主 (UI) 线程(如绘制屏幕),并且由于在主 (UI) 线程上调用onResume()您正在阻止 Android 在主 (UI) 上执行任何工作线。 This is why you see a white (or black) screen in this case.这就是在这种情况下您会看到白色(或黑色)屏幕的原因。

To answer your question, Activity will be visible after onResume() .要回答您的问题, Activity将在onResume()之后可见。

Please look at the activity life-cycle.请查看活动生命周期。

在此处输入图像描述

The activity is visible and running between onResume() and onPause() .该活动在onResume()onPause()之间可见并正在运行。 For more info refer this docs .有关更多信息,请参阅此文档

Now coming back to why you see white screen.现在回到为什么你会看到白屏。

Views are drawn asynchronously - meaning that execution of your code does not wait until views are drawn.视图是异步绘制的——这意味着代码的执行不会等到视图绘制完成。 If onResume() is called before all the views are rendered and your infinity loop starts - it will eat up all the computing power and your UI will not be rendered.如果在渲染所有视图之前调用onResume()并且您的无限循环开始 - 它会消耗所有计算能力并且您的 UI 将不会被渲染。

Let me make it more clear.让我说得更清楚。 Why you ask about views not showing when placing infinite loop in onResume ?为什么您在onResume中放置无限循环时询问未显示视图? why don't you ask why views are not created even though infinite loop comes after setContentView in onCreate itself?你为什么不问为什么即使在onCreate本身的setContentView之后出现无限循环也没有创建视图?

To further point-out the source of your misunderstanding -- you think that onStart and onResume are called only after views are created.进一步指出您误解的根源——您认为onStartonResume仅在创建视图后才被调用。 This is not true .不是真的 onStart and onResume can be called even before drawing of views is not completed. onStartonResume甚至可以在视图绘制未完成之前调用。 So, the situation could be that your views may exist in memory but are not yet rendered.因此,情况可能是您的视图可能存在于 memory 中但尚未呈现。

One more things worth noting还有一点值得注意

  1. It is not mandatory to call setContentView() in onCreate , you can call it even in onStart or onResumeonCreate中调用setContentView()不是强制性的,您甚至可以在onStartonResume中调用它

If for some reason you want to wait for a particulate view to be drawn before doing something, you must run your code in postRunnable如果由于某种原因你想在做某事之前等待一个特定的视图被绘制,你必须在 postRunnable 中运行你的代码

yourView.post(new Runnable({
   public void run(){
   // do your stuff
   }
}));

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

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