Created
November 19, 2020 20:37
-
-
Save damianesteban/2ec8112a820c89d3186533ce01399429 to your computer and use it in GitHub Desktop.
LiveDataActivity
This file contains 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
/* | |
* Copyright (C) 2019 The Android Open Source Project | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.android.example.livedatabuilder | |
import android.os.Bundle | |
import androidx.activity.viewModels | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.databinding.DataBindingUtil | |
import com.android.example.livedata.R | |
import com.android.example.livedata.databinding.ActivityLivedataBinding | |
class LiveDataActivity : AppCompatActivity() { | |
// Obtain ViewModel | |
private val viewmodel: LiveDataViewModel by viewModels { LiveDataVMFactory } | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
// Obtain binding object using the Data Binding library | |
val binding = DataBindingUtil.setContentView<ActivityLivedataBinding>( | |
this, R.layout.activity_livedata | |
) | |
// Set the LifecycleOwner to be able to observe LiveData objects | |
binding.lifecycleOwner = this | |
// Bind ViewModel | |
binding.viewmodel = viewmodel | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment