内からコンテキストを取得することが可能です Fragment
ただやる
public class YourFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
final View root = inflater.inflate(R.layout.yout_fragment_layout, container, false);
Context context = getContext();
SomeViewModel someViewModel = ViewModelProviders.of(this).get(SomeViewModel.class);
Context context = homeViewModel.getContext();
return root;
}
}
また、添付のことAndroidViewModel
でonCreateView
方法その実装アプリケーションコンテキストを返すメソッド
public class SomeViewModel extends AndroidViewModel {
private MutableLiveData<ArrayList<String>> someMutableData;
Context context;
public SomeViewModel(Application application) {
super(application);
context = getApplication().getApplicationContext();
someMutableData = new MutableLiveData<>();
.
.
}
public Context getContext() {
return context
}
}