Search code examples
androidkotlinandroid-lifecycle

Make Android Lifecycle Observer receiver non nullable in Kotlin


I have Result wrapper that wraps data comes from backend

data class Result<T>(val success: Boolean, val result: T?, val message: String?)

Idea of this, check success instead of result being null or not valid and get formatted message for UI error reporting. But when trying to use this with android lifestyle components, specifically in Observer I have to check for null.

enter image description here

How can I avoid this null check? This happens because of

void onChanged(@Nullable T t);

in Observer. I've tried to extend this but it seem to require more custom wrapper classes. Do we have a solution for avoid null check here.


Solution

  • It's a framework bug that argument is annotated as @Nullable. Fixed in androix.lifecycle 2.0.0-beta01.