get context SavedStateHandle Java Android

314 views Asked by At

I want to get something from SharedPreferences in a ViewModel class. The constructor has a SavedStateHandle parameter only and I don't know how to get the context. Here is my code:

public class PropertyDetailActivityViewModel extends ViewModel{

    public enum Event{
        Ok, Ko
    }

    public MutableLiveData<Event> event = new MutableLiveData<>();

    public MutableLiveData<Property> property = new MutableLiveData<>();

    private final Property propertyExtra;


    public PropertyDetailActivityViewModel(SavedStateHandle savedStateHandle) {
        propertyExtra = savedStateHandle.get(PropertyDetailActivity.PROPERTY_EXTRA);
        property.postValue(propertyExtra);
    }

    public void deleteProperty()
    {
        if(AppPreferences.getAgentName(//How to get context here?) == propertyExtra.agent)
        {

        }
    }
}

viewModel definition in the view class:

viewModel = new ViewModelProvider(this, new SavedStateViewModelFactory(getApplication(), this, getIntent().getExtras())).get(PropertyDetailActivityViewModel.class);
0

There are 0 answers