Last active
June 20, 2019 19:25
-
-
Save ceruleanotter/6e226b82bd106a10c97f96412d18206d to your computer and use it in GitHub Desktop.
ViewModel Integrations: Data Binding Example 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
// This ktx requires at least androidx.activity:activity-ktx:1.0.0 | |
private val myViewModel: MyViewModel by viewModels() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
//Inflate view and create binding | |
val binding: MainActivityBinding = | |
DataBindingUtil.setContentView(this, R.layout.main_activity) | |
//Specify this activity as the lifecycleOwner for Data Binding | |
binding.lifecycleOwner = this | |
// Pass the ViewModel into the binding | |
binding.viewmodel = myViewModel | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment