如何使用活动A的MutableLiveData viewModel到活动性B的viewModel

时间:2019-11-06 09:59:28

标签: android android-livedata

我想在以后的活动中使用相同的MutableLiveData。

public class DocumentDetailViewModel extends BaseViewModel<DocumentDetailNavigator> {

private final MutableLiveData<DocumentDetail> documentDetailTicketsListLiveData;
private int docId;


public ObservableField<String> caseNumber;

public DocumentDetailViewModel(DataManager dataManager,
                               SchedulerProvider schedulerProvider) {
    super(dataManager, schedulerProvider);
    documentDetailTicketsListLiveData = new MutableLiveData<>();
    fetchDocumentDetails();
}

public void fetchDocumentDetails() {
    setIsLoading(true);
    getCompositeDisposable().add(getDataManager()
            .getDocumentDetailAppCall(docId)
            .subscribeOn(getSchedulerProvider().io())
            .observeOn(getSchedulerProvider().ui())
            .subscribe(DocumentDetailResponse -> {
                if (DocumentDetailResponse != null) {
                    documentDetailTicketsListLiveData.setValue(DocumentDetailResponse);

                    caseNumber = new ObservableField<>(DocumentDetailResponse.getDocuments().getCaseNumber());
                }
                setIsLoading(false);
            }, throwable -> {
                setIsLoading(false);
                getNavigator().handleError(throwable);
            }));
}

public LiveData<DocumentDetail> getDocumentDetailLiveData() {
    return documentDetailTicketsListLiveData;
}

public void setDocId(int docId) {
    this.docId = docId;
}
}  

我想在其他活动中访问documentDetailTicketsListLiveData。

0 个答案:

没有答案