Skip to content

Instantly share code, notes, and snippets.

@burhanrashid52
Last active November 15, 2017 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burhanrashid52/031e7e13e0469953f645f14be1685b96 to your computer and use it in GitHub Desktop.
Save burhanrashid52/031e7e13e0469953f645f14be1685b96 to your computer and use it in GitHub Desktop.
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