hammadahmed85
hammadahmed85

Reputation: 23

Setting liveData from a custom application class

I am adding a LiveData to represent a status. This is the ViewModel class:

public class MyViewModel extends AndroidViewModel {
    // Create a LiveData with a Status
    private MutableLiveData<Integer> status;

    public TsViewModel(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<Integer> getCurrentStatus() {
        if (status == null) {
            status = new MutableLiveData<Integer>();
        }
        return status;
    }
    void setCurrentStatus(int stat) {
        status.setValue(stat);
    }
}

The current status is set in native code. I want this status to be updated in the custom application class. But it seems like I can't get a viewModel from that class:

public class MyApplication extends Application {

    public static Context context;
    public void onCreate() {
        super.onCreate();
        MyApplication .context = getApplicationContext();
        model = new ViewModelProvider(this).get(MyViewModel.class);  /* <---- this fails */
    }
}

The idea really is to set this status as early possible and that any observer would know. How can I acheive this?

Upvotes: 0

Views: 1169

Answers (0)

Related Questions