Android Development

Dagger Android Tutorial 3

How to do dagger-android

Guowei Lv

1 minute read

Inject Retrofit @Singleton The Retrofit instance should be a singleton in the scope of AppComponent. Provide the Retrofit instance in AppModule. @Singleton @Provides static Retrofit provideRetrofitInstance() { return new Retrofit.Builder().baseUrl(Constants.BASE_URL) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()).build(); } Under AuthActivity subcomponent, create a module called AuthModule, in it, provides the AuthApi instance. @Module public class AuthModule { @Provides static AuthApi provideAuthApi(Retrofit retrofit) { return retrofit.create(AuthApi.class); } } public interface AuthApi { @GET("/users/{id}") Flowable<User> getUser(@Path("id") int id); } @ContributesAndroidInjector(modules = {AuthModule.

Dagger Android Tutorial 2

How to do dagger-android

Guowei Lv

3 minute read

In this article we show how to inject into ViewModels. Note that we will not dive too deep into the inner workings of things, since they are extremely complex. If you want to get some level of insight into the topic read this article. But I try to give the clean solution and how you can put it in your head. First a few words about the ViewModel in android MVVM architecture.

Dagger Android Tutorial 1

How to do dagger-android

Guowei Lv

2 minute read

Warning: Google thinks you should avoid dagger-android for new projects. Watch Here But, if you are using it now, here are some notes on how to approach it. This is basically a text version of these videos Application Component Pretty much every app will have an application component, whose scope will be the lifetime of the application. First, create the AppComponent //1. Include this 'AndroidSupportInjectionModule', this is required @Component(modules = {AndroidSupportInjectionModule.

Instance Initialization Block in Android

Use of IIB block in Android

Guowei Lv

2 minute read

This will be a short and sweet post. The usage of Instance Initialization Block of Java in custom android views. First of all, I have a confession to make. After almost 10 years of Java programming, I do not really know what is IIB and why it exists. Pretty obviously, the purpose of IIB is to initialize instance variables of a class. But you may ask isn’t that the job of constructors?

Where to Put observeOn in Rxjava2

Where to put observeOn matters!

Guowei Lv

1 minute read

Me: Why I have put the observeOn(AndroidSchedulars.mainThread()) but still onNext() is NOT called in android main thread?! Rx Master: Show me your code. Me: Here you are my master. I just want to wait for 5 seconds then call an api: override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) fun networkObservable(): Observable<String> { return Observable.just("test").subscribeOn(Schedulers.io()) } Observable.timer(5, TimeUnit.SECONDS) .observeOn(AndroidSchedulers.mainThread()) .flatMap { _ -> networkObservable() } .subscribe(Consumer { Log.d("testtest", it) Log.d("testtest", Thread.currentThread().name) }) } } Rx Master: … Read the Doc of observeOn: