Orhan Tozan
04/14/2020, 1:17 PMinit {}
called? I know that LiveData's only get initialized after observing them, but what about the init block? On Activity onCreate?aipok
04/14/2020, 1:38 PMinit
will be called first time ViewModel is created. For example when it is being injected or when you create it from factory and it will not be called in case fragment get it as a shared instance.Orhan Tozan
04/14/2020, 1:38 PMaipok
04/14/2020, 1:41 PMinit
will be called in the same method where you call it for the first time. In case it is in Activity:onCreate, then it will be called there.Orhan Tozan
04/14/2020, 1:42 PMaipok
04/14/2020, 1:45 PMlate init
in your activity using get()
from Koin. Not sure that calling the viewModel.toString is a good idea, since some other developer could break it very easy, he might think that it is some leftover from debugging. I had such cases in past 😄Orhan Tozan
04/14/2020, 1:46 PMaipok
04/14/2020, 1:49 PMprivate lateinit var viewModel: HomeViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = getViewModel<HomeViewModelUsingEngine>()
}
Matt Thompson
04/14/2020, 3:43 PMOrhan Tozan
04/14/2020, 6:01 PMBruno_
04/15/2020, 8:23 AMclass Foo(private val string: String) {
init {
val a = 2 + 2
println(a)
println(string)
}
}
you'll get the following thing
public final class Foo {
private final String string;
public Foo(@NotNull String string) {
Intrinsics.checkParameterIsNotNull(string, "string");
super();
this.string = string;
int a = 4;
boolean var3 = false;
System.out.println(a);
String var5 = this.string;
boolean var4 = false;
System.out.println(var5);
}
}
myanmarking
04/15/2020, 1:29 PMinit {} is not a viewModel thing, but kotlin's isnt it? Init is called just after constructor return