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

今後のJetpackでAndroid開発はこう変わる!

 今後のJetpackでAndroid開発はこう変わる!

CA.apk #8

Keita Kagurazaka

September 10, 2019
Tweet

More Decks by Keita Kagurazaka

Other Decks in Programming

Transcript

  1. これまでのFragmentのbackハンドリング interface OnBackPressedListener { fun onBackPressed(): Boolean } // in

    Activity override fun onBackPressed() { supportFragmentManager.fragments.reversed().forEach { if (it.isVisible && it is OnBackPressedListener) { if (it.onBackPressed()) return } } super.onBackPressed() }
  2. これからのFragmentのbackハンドリング // in Fragment override fun onAttach(context: Context) { super.onAttach(context)

    val onBackPressedCallback = requireActivity().onBackPressedDispatcher.addCallback(this) { // backが押されたときの処理 } Handler().postDelayed({ onBackPressedCallback.isEnabled = false }, 2000L) }
  3. これからのFragmentのbackハンドリング // in Fragment override fun onAttach(context: Context) { super.onAttach(context)

    val onBackPressedCallback = requireActivity().onBackPressedDispatcher.addCallback(this) { // backが押されたときの処理 } Handler().postDelayed({ onBackPressedCallback.isEnabled = false }, 2000L) } LifecycleOwner
  4. これからのFragmentのbackハンドリング // in Fragment override fun onAttach(context: Context) { super.onAttach(context)

    val onBackPressedCallback = requireActivity().onBackPressedDispatcher.addCallback(this) { // backが押されたときの処理 } Handler().postDelayed({ onBackPressedCallback.isEnabled = false }, 2000L) } callbackは有効なものが 登録逆順で呼び出される
  5. これまでのViewModel取得方法 // in Activity or Fragment private val viewModel: UserDetailViewModel

    by lazy { ViewModelProviders.of(this).get(UserDetailViewModel::class.java) } // in Fragment private val activityViewModel: UserDetailViewModel by lazy { ViewModelProviders.of(requireActivity()).get(UserDetailViewModel::class.java) } // in Fragment in Fragment private val parenttViewModel: UserDetailViewModel by lazy { ViewModelProviders.of(requireParentFragment()) .get(UserDetailViewModel::class.java) }
  6. これからのViewModel取得方法 // in Activity or Fragment private val viewModel: UserDetailViewModel

    by viewModels() // in Fragment private val activityViewModel: UserDetailViewModel by activityViewModels() // in Fragment in Fragment private val viewModel: UserDetailViewModel by viewModels({ requireParentFragment() })
  7. viewModelsの中身 @MainThread inline fun <reified VM : ViewModel> Fragment.viewModels( noinline

    ownerProducer: () -> ViewModelStoreOwner = { this }, noinline factoryProducer: (() -> Factory)? = null ) = createViewModelLazy(VM::class, { ownerProducer().viewModelStore }, factoryProducer)
  8. viewModelsの中身 @MainThread inline fun <reified VM : ViewModel> Fragment.viewModels( noinline

    ownerProducer: () -> ViewModelStoreOwner = { this }, noinline factoryProducer: (() -> Factory)? = null ) = createViewModelLazy(VM::class, { ownerProducer().viewModelStore }, factoryProducer) ViewModelのscopeを決める
  9. viewModelsの中身 @MainThread inline fun <reified VM : ViewModel> Fragment.viewModels( noinline

    ownerProducer: () -> ViewModelStoreOwner = { this }, noinline factoryProducer: (() -> Factory)? = null ) = createViewModelLazy(VM::class, { ownerProducer().viewModelStore }, factoryProducer) ViewModelのFactoryを指定する
  10. これまでのSavedStateのハンドリング in ViewModel // in Fragment override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) savedInstanceState?.let(viewModel::onRestoreInstanceState) } override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) viewModel.onSaveInstanceState(outState) }
  11. これからのSavedStateのハンドリング in ViewModel class DetailViewModel( private val savedStateHandle: SavedStateHandle )

    : ViewModel() { fun someProcess() { savedStateHandle.set("key_value", 100) val saved = savedStateHandle.get<Int>("key_value") } }
  12. これからのSavedStateのハンドリング in ViewModel class DetailViewModel( private val savedStateHandle: SavedStateHandle )

    : ViewModel() { fun someProcess() { savedStateHandle.set("key_value", 100) val saved = savedStateHandle.get<Int>("key_value") } } Bundleに入る値なら入れられる
  13. これからのSavedStateのハンドリング in ViewModel class DetailViewModel( private val savedStateHandle: SavedStateHandle )

    : ViewModel() { fun someProcess() { savedStateHandle.set("key_value", 100) val saved = savedStateHandle.get<Int>("key_value") } } Daggerの場合 -> Ask me later!
  14. これまでのLiveData transformations data class User(val name: String) // in ViewModel

    private val mutableUser = MutableLiveData<User>() val user: LiveData<String> = Transformations.map(mutableUser) { it.name }
  15. これからのLiveData transformations data class User(val name: String) // in ViewModel

    private val mutableUser = MutableLiveData<User>() val user: LiveData<String> = mutableUser.map { it.name } .distinctUntilChanged()
  16. LiveData transformations data class User(val name: String) // in ViewModel

    private val mutableUser = MutableLiveData<User>() val user: LiveData<String> = mutableUser.map { it.name } .distinctUntilChanged() ktxにLiveDataの 拡張関数として定義された
  17. LiveData transformations data class User(val name: String) // in ViewModel

    private val mutableUser = MutableLiveData<User>() val user: LiveData<String> = mutableUser.map { it.name } .distinctUntilChanged() 待望のdistinctUntilChanged追加
  18. これまでのcoroutine起動 in ViewModel class DetailViewModel : ViewModel(), CoroutineScope { private

    val job = SupervisorJob() override val coroutineContext: CoroutineContext = Dispatchers.Main.immediate + job override fun onCleared() { cancel() } fun onClicked() { launch { // 色々 } } }
  19. これからのcoroutine起動 in ViewModel // in ViewModel fun onClicked() { viewModelScope.launch

    { // 色々 } } onCleared()でキャンセルされる CoroutineScope
  20. これまでのcoroutine起動 in LifecycleOwner class DetailFragment : Fragment(), CoroutineScope { private

    lateinit var job: Job override val coroutineContext get() = Dispatchers.Main + job override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) job = SupervisorJob() launch { … } } override fun onDestroy() { cancel() super.onDestroy() } }
  21. これからのcoroutine起動 in LifecycleOwner class DetailFragment : Fragment() { override fun

    onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) lifecycleScope.launch { // 色々 } } }
  22. これからのcoroutine起動 in LifecycleOwner class DetailFragment : Fragment() { override fun

    onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) lifecycleScope.launch { // 色々 } } } onDestroy()でキャンセルされる CoroutineScope
  23. これからのcoroutine起動 in LifecycleOwner class DetailFragment : Fragment() { init {

    lifecycleScope.launchWhenStarted { // Fragment transactionなどのライフサイクル制限がある処理 } } }
  24. これからのcoroutine起動 in LifecycleOwner class DetailFragment : Fragment() { init {

    lifecycleScope.launchWhenStarted { // Fragment transactionなどのライフサイクル制限がある処理 } } } onStart()で開始し、 onStop()でキャンセルされる coroutineを起動