Skip to content Skip to sidebar Skip to footer

Android Custom Dialog With Mvvm

I am creating custom dialog and i want when the user click add button go and call retrofit and observe on changes but i don't know how to pass lifecycleowner to the observer priva

Solution 1:

Try this solution. It worked for me.

Create a field of your activity from where you are calling the dialog and pass this in place of lifecycleowner

publicclassYourDialogextendsDialogFragment {

privateYourActivity activity;

    publicstaticYourDialognewInstance(YourActivity activity) {
        YourDialog dialog = newYourDialog();
        dialog.activity = activity;
        return dialog;
    }

    privatevoidobserveViewModel(ProjectListViewModel viewModel) {
    // Update the list when the data changes
    viewModel.getProjectListObservable().observe( activity , newObserver<List<Project>>() {
        @OverridepublicvoidonChanged(@Nullable List<Project> projects) {
            if (projects != null) {
                //…
                projectAdapter.setProjectList(projects);
            }
        }
    });

}

You can refer the example of mvvm here if you want

Post a Comment for "Android Custom Dialog With Mvvm"