Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Register application as context in LiveData

I am trying to register the application to the observer but I get this message:

requires: 'android.arch.lifecycle.LifecycleOwner

Any ideas?

public class CompanyMV extends AndroidViewModel{

public CompanyMV(Application application) {

    super(application);
    repo.getNumbers(this.ID).observe(application, new Observer<Integer>() {
        @Override
        public void onChanged(@Nullable Integer number) {

        }
    });

}
like image 583
Nick Avatar asked Aug 14 '18 22:08

Nick


Video Answer


1 Answers

observe() is for use by activities, fragments, and other things with a discrete lifecycle. An Application does not have that — it lives as long as your process does.

Use observeForever() instead of observe() here.

like image 157
CommonsWare Avatar answered Oct 13 '22 07:10

CommonsWare