lifecycle-ktx | Several Kotlin extensions for convenient use
kandi X-RAY | lifecycle-ktx Summary
kandi X-RAY | lifecycle-ktx Summary
lifecycle-ktx
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of lifecycle-ktx
lifecycle-ktx Key Features
lifecycle-ktx Examples and Code Snippets
Community Discussions
Trending Discussions on lifecycle-ktx
QUESTION
There are two similar extension method in androidX lifecycle-ktx
package with slightly different signature. withStateAtLeast
and whenStateAtLeast
. I read the doc but couldn't understand the difference in their behavior. An example on when we should use which one, would be appreciated.
ANSWER
Answered 2021-Apr-18 at 06:20Looking at the source code, whenStateAtLeast runs the given block when the lifecycle is at least in the required state and suspends the block if the lifecycle moves to lesser state while block is running.
At the same time withStateAtLeast just waits for the lifecycle to be at least in the required state and runs the block. So it guarantees that the lifecycle state meets the requirement at the time it starts the block, but if the block suspends, by the time it's resumed, the lifecycle can be in lesser state or even destroyed.
So in general withStateAtLeast
is a good choice if you need to run a block when lifecycle reaches the state (e.g. user entered the screen) and finish its execution in any case, even if the user leaves the screen before the execution finished. whenStateAtLeast
in its turn is useful when the block works with UI, it guarantees, that each time it resumes execution, the lifecycle is in proper state, so you can access UI safely.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install lifecycle-ktx
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page