public class NameActivity extends AppCompatActivity { | |
private NameViewModel mModel; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// Get the ViewModel. | |
mModel = ViewModelProviders.of(this).get(NameViewModel.class); | |
// Create the observer which updates the UI. | |
final Observer<String> nameObserver = new Observer<String>() { | |
@Override | |
public void onChanged(@Nullable final String newName) { | |
// Update the UI, in this case, a TextView. | |
mNameTextView.setText(newName); | |
} | |
}; | |
// Observe the LiveData, passing in this activity as the LifecycleOwner and the observer. | |
mModel.getCurrentName().observe(this, nameObserver); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment