Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Architecture Componentsで犯しがちな間違い / Comm...

D.Forest
August 27, 2019

Android Architecture Componentsで犯しがちな間違い / Common mistakes when using Android Architecture Components

D.Forest

August 27, 2019
Tweet

Other Decks in Programming

Transcript

  1. 2

  2. class BooksFragment: Fragment() { private lateinit var viewModel: BooksViewModel override

    fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.fragment_books, container) } override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) viewModel = ViewModelProviders.of(this).get(BooksViewModel::class.java) viewModel.liveData.observe(this, Observer { updateViews(it) }) } ... }
  3. class BooksFragment: Fragment() { private lateinit var viewModel: BooksViewModel override

    fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.fragment_books, container) } override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) viewModel = ViewModelProviders.of(this).get(BooksViewModel::class.java) viewModel.liveData.observe(this, Observer { updateViews(it) }) } ... }
  4. class BooksFragment: Fragment() { private lateinit var viewModel: BooksViewModel override

    fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.fragment_books, container) } override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) viewModel = ViewModelProviders.of(this).get(BooksViewModel::class.java) viewModel.liveData.observe(viewLifecycleOwner, Observer { updateViews(it) }) } ... }
  5. private val promotions = MutableLiveData<Promotions>() fun getPromotions(): LiveData<Promotions> = promotions

    private val _offers = MutableLiveData<Offers>() val offers: LiveData<Offers> = _offers OR