bufferapp / clean-architecture-components-boilerplate

A fork of our clean architecture boilerplate, this time using the Android Architecture Components
MIT License
1.28k stars 178 forks source link

BrowseBufferoosViewModel doesn't survive configuration change #3

Closed vlazzle closed 7 years ago

vlazzle commented 7 years ago

Steps to repro:

  1. Set a breakpoint or log in BrowseBufferoosViewModel.init
  2. Build & deploy
  3. Rotate device
  4. Observe that the log or breakpoint in BrowseBufferoosViewModel.init is hit for every rotation

android.arch.lifecycle.ViewModels are meant to survive config changes.

BrowseBufferoosViewModel will survive config changes if it's retrieved in BrowseActivity the normal way using ViewModelProviders.of. If you prefer to inject with dagger, take a look at this sample, then retrieve the ViewModel like so

hitherejoe commented 7 years ago

Awesome spot, thanks! Now injecting a Factory for the ViewModel (https://github.com/bufferapp/clean-architecture-components-boilerplate/blob/master/presentation/src/main/java/org/buffer/android/boilerplate/presentation/browse/BrowseBufferoosViewModelFactory.kt) and then using this when fetching the ViewModel from the provider (https://github.com/bufferapp/clean-architecture-components-boilerplate/blob/master/mobile-ui/src/main/java/org/buffer/android/boilerplate/ui/browse/BrowseActivity.kt#L34)

Thanks again!

vlazzle commented 7 years ago

Great! You're very welcome

On Mon, Sep 25, 2017, 9:33 PM Joe Birch notifications@github.com wrote:

Closed #3 https://github.com/bufferapp/clean-architecture-components-boilerplate/issues/3 .

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/bufferapp/clean-architecture-components-boilerplate/issues/3#event-1263511460, or mute the thread https://github.com/notifications/unsubscribe-auth/AABQdTf_ML1uk6coRFwuzQOWGYOCnBiaks5sl4-jgaJpZM4PhuPS .