上一篇文章讲到ViewModel如何使用,这篇文章讲LiveDate
android开发:JetPack之ViewModel(一)
简介:
LiveDate是可以被观察的数据持有者,LiveDate数据发生变化时候可以通知activity、fragment。
当activity、fragment作为观察者去观察LiveData后,当观察者的生命周期处于STARTED或RESUMED状态时(即onStart()、onResume()、onPause()),LiveData的数据发生变化,则会通知观察者;若观察者处于其他状态,即使LiveData的数据发生变化,也不会发出通知。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView textView;
MyViewModel myViewModel;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate