Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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().

Also in this one you can see:

The onStart() call makes the activity visible to the user ...

So I thought the UI of Activity must be visible after returning from onStart().

Recently, for testing purpose, I put an infinite loop in onResume() and guessed that the UI of Activity must be visible. But the result of starting Activity was a white window without any UI.

So it seems that UI of Activity will be visible after returning from onResume() instead of returning from onStart(). Is this true? If yes, why docs do say such misleading statements?

Edit:

You may think like Công Hải which says:

I think the documents mention about window visible not view visible.

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. So "visibility of Activity" must mean another thing than "white window".

Edit2

Alongside official docs, many tutorials say the Activity will be visible by calling onStart() and interactive by calling onResume(). Are they all say incorrect thing without testing?

like image 650
hasanghaforian Avatar asked Jul 31 '20 06:07

hasanghaforian


People also ask

What is the relationship between onStart () and onResume ()?

onStart() -> called when the activity becomes visible, but might not be in the foreground (e.g. an AlertFragment is on top or any other possible use case). onResume() -> called when the activity is in the foreground, or the user can interact with the Activity. Save this answer.

What's the order in which the activity calls onResume () onCreate () and onStart () assuming the activity is being launched for the first time?

As discussed, the onCreate() function is only called once in an activity's life-cycle. So, when the onRestart() method is executed, the activity will resume by executing the onStart() then onResume() .

Is it mandatory to implement onCreate () and onStart () of activity life cycle will it run if those methods are removed?

Is it mandatory to implement onCreate() and onStart() of activity life cycle? Will it run if those methods are removed? Ans. Not mandatory.

What is the difference between onCreate () and onStart () learn from here?

onCreate() is called when the when the activity is first created. onStart() is called when the activity is becoming visible to the user.


2 Answers

Putting an infinite loop in onResume() is not really going to tell you the whole story. 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. This is why you see a white (or black) screen in this case.

To answer your question, Activity will be visible after onResume().

like image 85
David Wasser Avatar answered Oct 19 '22 22:10

David Wasser


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. Such activity is completely alive (it maintains all state and member information and remains attached to the window manager).

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.

[...]

The visible lifetime of an activity happens between a call to onStart() until a corresponding call to 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. The onStart() and onStop() methods can be called multiple times, as the activity becomes visible and hidden to the user.

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.

like image 45
ymindstorm Avatar answered Oct 19 '22 21:10

ymindstorm