Skip to content
Permalink
Browse files

Use viewLifecycleOwner for LiveData observation

  • Loading branch information
zsmb13 committed Jan 7, 2020
1 parent b2c294c commit 95ebf9166602014ff68b055f29fb07d9ab3f67c9
@@ -35,8 +35,7 @@ class DashboardFragment : Fragment() {
val root = inflater.inflate(R.layout.fragment_dashboard, container, false)

arguments?.getString("id").let {

dashboardViewModel.getPokemonById(it).observe(this, Observer { list ->
dashboardViewModel.getPokemonById(it).observe(viewLifecycleOwner, Observer { list ->
list?.get(0).let { pokemon ->
root.textViewID.text = pokemon?.id
root.textViewName.text = pokemon?.name
@@ -38,7 +38,7 @@ class AboutFragment : Fragment() {

arguments?.getString("id").let {

dashboardViewModel.getPokemonById(it).observe(this, Observer { list ->
dashboardViewModel.getPokemonById(it).observe(viewLifecycleOwner, Observer { list ->
list?.get(0).let { pokemon ->

root.textViewDescription.text = pokemon?.xdescription
@@ -38,7 +38,7 @@ class StatsFragment : Fragment() {

arguments?.getString("id").let {

dashboardViewModel.getPokemonById(it).observe(this, Observer { list ->
dashboardViewModel.getPokemonById(it).observe(viewLifecycleOwner, Observer { list ->
list?.get(0).let { pokemon ->

root.textViewTypeDefenses.text = pokemon?.ydescription
@@ -31,7 +31,7 @@ class GenerationFragment : BottomSheetDialogFragment() {
val layoutManager = GridLayoutManager(context, 2)
recyclerView.layoutManager = layoutManager

generationViewModel.getListGeneration().observe(this, Observer {
generationViewModel.getListGeneration().observe(viewLifecycleOwner, Observer {
val pokemons: List<Generation> = it
recyclerView.adapter = GenerationAdapter(pokemons, root.context)
})
@@ -46,12 +46,12 @@ class HomeFragment : Fragment() {
)
)

homeViewModel.getListMenu().observe(this, Observer {
homeViewModel.getListMenu().observe(viewLifecycleOwner, Observer {
val items: List<Menu> = it
recyclerViewMenu.adapter = MenuAdapter(items, root.context)
})

homeViewModel.getListNews().observe(this, Observer {
homeViewModel.getListNews().observe(viewLifecycleOwner, Observer {
val items: List<News> = it
recyclerViewNews.adapter = NewsAdapter(items, root.context)
})
@@ -40,7 +40,7 @@ class PokedexFragment : Fragment() {
val layoutManager = GridLayoutManager(context, 2)
recyclerView.layoutManager = layoutManager

pokedexViewModel.getListPokemon().observe(this, Observer {
pokedexViewModel.getListPokemon().observe(viewLifecycleOwner, Observer {
val pokemons: List<Pokemon> = it
recyclerView.adapter = PokemonAdapter(pokemons, root.context)
if (pokemons.isNotEmpty())

0 comments on commit 95ebf91

Please sign in to comment.
You can’t perform that action at this time.