내에서 컨텍스트를 얻을 수 있습니다. 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
}
}