package keijumt.devandroid.fragment import android.arch.lifecycle.Observer import android.arch.lifecycle.ViewModelProviders import android.content.Intent import android.databinding.DataBindingUtil import android.os.Bundle import android.support.v4.app.FragmentActivity import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import keijumt.devandroid.R import keijumt.devandroid.activity.ApiActivity import keijumt.devandroid.activity.AsyncActivity import keijumt.devandroid.databinding.FragmentMainBinding import keijumt.devandroid.viewmodel.MainViewModel class MainFragment : BaseFragment() { lateinit var binding: FragmentMainBinding lateinit var mainViewModel: MainViewModel override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val nonNullActivity: FragmentActivity = activity ?: return null binding = DataBindingUtil.inflate(inflater, R.layout.fragment_main, null, false) mainViewModel = ViewModelProviders.of(nonNullActivity, viewModelFactory).get(MainViewModel::class.java) binding.mainViewModel = mainViewModel subscribe() return binding.root } fun subscribe() { mainViewModel.event.observe(this, Observer { when (it) { MainViewModel.Event.Api -> startActivity(Intent(activity, ApiActivity::class.java)) MainViewModel.Event.Async -> startActivity(Intent(activity, AsyncActivity::class.java)) } }) } }