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

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

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. Android Architecture Components
    $ Cooking Programmer

    View full-size slide

  2. Cooking Programmer
    @d_forest
    Android Engineer
    Software Engineer
    https://prty.jp

    View full-size slide

  3. Android Architecture Components

    View full-size slide

  4. https://proandroiddev.com/5-common-mistakes-when-using-architecture-components-403e9899f4cb

    View full-size slide

  5. https://note.mu/d_forest/n/n9c2dcf155f34

    View full-size slide

  6. Fragment LiveData observer
    ViewModel
    Mutable LiveData View
    ViewModel

    View full-size slide

  7. Fragment LiveData observer

    View full-size slide

  8. 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) })
    }
    ...
    }

    View full-size slide

  9. 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) })
    }
    ...
    }

    View full-size slide

  10. viewModel.liveData.observe(this, Observer { updateViews(it) })
    Activity
    Fragment
    e.g. detach onDestroy() Observer
    Fragment attach/detach Observer

    View full-size slide

  11. viewModel.liveData.observe(viewLifecycleOwner, Observer {...

    public class Fragment {
    public LifecycleOwner getViewLifecycleOwner()
    }

    View full-size slide

  12. 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) })
    }
    ...
    }

    View full-size slide

  13. Mutable LiveData View

    View full-size slide

  14. class CatalogueViewModel : ViewModel() {
    val products = MutableLiveData()
    }

    View full-size slide

  15. class CatalogueViewModel : ViewModel() {
    val products = MutableLiveData()
    }

    View full-size slide

  16. val products = MutableLiveData()
    View LiveData

    View full-size slide

  17. private val promotions = MutableLiveData()
    fun getPromotions(): LiveData = promotions
    private val _offers = MutableLiveData()
    val offers: LiveData = _offers
    OR


    View full-size slide

  18. class CatalogueViewModel : ViewModel() {
    private val _offers = MutableLiveData()
    val offers: LiveData = _offers
    }

    View full-size slide