injectedvmprovider

Introduction: Small lib to use easily use Android's ViewModels with a depedency injection framework like dagger
More: Author   ReportBugs   
Tags:

This is a small lib to use easily use Android's ViewModels with a depedency injection framework like dagger. You obtain a ViewModel from a javax.inject.Provider instead of a ViewModelProvider.Factory.

Usage

From Java

Download

implementation 'me.tatarka.injectedvmprovider:injectedvmprovider-extensions:1.0'

For androidx, use version 2.0 instead.

Usage

public class MyActivity extends AppCompatActivity {

    @Inject
    Provider<MyViewModel> vmProvider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        MyViewModel vm = InjectedViewModelProviders.of(this).get(vmProvider);
    }
}

Note: If you aren't using fragments, you can use me.tatarka.injectedvmprovider:injectedvmprovider:1.0, and use new InjectedViewModelProvider(viewModelStoreOwner) instead.

From Kotlin

Download

implementation 'me.tatarka.injectedvmprovider:injectedvmprovider-ktx:1.0'

For androidx, use version 2.0 instead.

Usage

class KotlinDaggerMainActivity : AppCompatActivity() {

    @Inject
    lateinit var vmProvider: Provider<MainViewModel>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ...
        val vm = injectedViewModelProvider[vmProvider]
    }
}
Support Me
Apps
About Me
Google+: Trinea trinea
GitHub: Trinea