How to handle Android LiveData(ViewModel) with ProgressBar on screen rotate?

android multiple viewmodels
observe livedata in viewmodel
viewmodel to view communication android
android livedata remove observer
android mvvm livedata
progressbar mvvm android
update ui from viewmodel android
android nested livedata

onChanged I am updating the UI and cancelling the progressDialog but each time I am rotating the screen the progress dialog shows up and ProgressDialog doesn't dismiss.

How to handle the progressdialog if the data is not changed?

 final ProgressDialog pd;
    pd=new ProgressDialog(mContext);
    pd.setMessage(mContext.getString(R.string.updating));
    pd.show();MutableLiveData<BillGetResponse> DisplayByIdLiveData=viewModelForBillDisplay.getBillGetResponseDisplayById(txnRequest);

    if (display_by_id) {


        DisplayByIdLiveData.observe(BillDisplayFragment.this, new Observer<BillGetResponse>() {


            @Override
            public void onChanged(@Nullable BillGetResponse billGetResponse) {


                if (btnRecordDisableEnabled != null)
                    btnRecordDisableEnabled.enable();

                pd.dismiss();  }

but everytime the onChanged is not trigerred and the progressdialog doesnot stop !!

You need other observer to handle the value of the boolean that indicates if you have to show the ProgressDialog or not. This is the code:

viewModel.getIsLoading().observe(this, new Observer<Boolean>() {
        @Override
        public void onChanged(@Nullable Boolean aBoolean) {
            if(aBoolean){
                pd.show();
            }else{
                pd.dismiss();
            }
        }
    });

In your viewModel add getIsLoading method:

public LiveData<Boolean> getIsLoading(){
    LiveData<Boolean> isLoading=yourRepository.getIsLoading();
    return isLoading;
}

And in your repository add:

private final MutableLiveData<Boolean> isLoading=new MutableLiveData<>();

and set value to isLoading when start and finish the data upload.

I hope it helps you.

ViewModels and LiveData: Patterns + AntiPatterns, on rotating the screen will recreate your activity. To stop recreating it use android:​configChanges="keyboardHidden|orientation". in manifest file for your activity  All the course codelabs are listed on the Android Kotlin Fundamentals codelabs landing page. This codelab is part of a four-codelab series that teaches you about ViewModel and LiveData . ViewModel and LiveData are Android Architecture Components , which are libraries and other components that help you design robust, testable, maintainable apps.

onChanged() not getting called is perfectly normal, it means there are no more changes in data.

I guess you want to show ProgressDialog while data is being fetched or loaded. You can do something like this

In onCreate() Check data is present or not before displaying, below in Pseudo code for displaying ProgressDialog

if (!hasData())
   displayProgressDialog()

EDIT: It seems you want to inform user that data has changed by means of showing ProgressDialog, You can achieve this by showing this for small moment and dismiss it after a while, Something like this in onChanged()

    @Override
    public void onChanged(@Nullable BillGetResponse billGetResponse) {
        if (btnRecordDisableEnabled != null)
            btnRecordDisableEnabled.enable();

        displayProgressDialog(); //Show progress dialog here
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                pd.dismiss();   
            }
        },500);
    }

Basic Example of LiveData and ViewModel, Ideally, ViewModels shouldn't know anything about Android. Views should only know how to display data and send user events to the ViewModel (or Presenter). source for data (such as a database or the network) when a rotation happens. We recommend you treat your events as part of your state. The next bit is where we make this work. First we want to ignore null values and early return. If the LiveData value is not null we notify the observer and set the value back to null straight away. This means that the state of the LiveData is always null and the LiveData only ever emits new actions when the value is set again.

on rotating the screen will recreate your activity. To stop recreating it use

android:configChanges="keyboardHidden|orientation" 

in manifest file for your activity like

<activity android:name="MainActivity"
     android:configChanges="keyboardHidden|orientation">

Using ViewModel and LiveData in your Android App – KdotJ.com, Life saving LiveData class Android When the application launch there will be progressbar which will be invisible after 5 seconds and list of fruits will be shown. Now when the orientation change is has not been handled then new instance of xmlns:app="http://schemas.android.com/apk/res-auto" Part 5 is where I first used LiveData as an architecture component, where I showed how LiveData can be used as a simple lifecycle-aware alternative to RxJava for implementing the observer pattern. The architecture components, however, feature a component for the ViewModel, called ViewModel , as well.

How To Use Android ViewModel – Baraa Bytes, This means objects are not recreated everytime the screen rotates or your app is backgrounded(unless of course, the system kills your app to  You’ll want to create a member variable for your ViewModel in the UI Controller. Then in onCreate, you should call: ViewModelProviders.of (<Your UI controller>).get (<Your ViewModel>.class) In

Android MVVM LiveData Data Binding, ViewModel is a class that is designed to store and manage UI related data. And all of that was just to survive the moment when the user rotates his device. the LiveData another awesome data class that available in Android final String TAG = "MovieViewModel"; // to show a progressbar while loading  If viewmodel wasn’t lifecycle aware, after the screen rotation, would be necessary to load the view data again because the viewmodel would be recreated together with the view. Another example: If the activity is finished while viewmodel was still loading some data, then when over, viewmodel won’t return the data to view because it knows that the view component was already finished, avoid some memory leak.

Working With LiveData, Today, we'll use LiveData with Data Binding in our MVVM Android Application. We'll see how LiveData makes it easy to update the UI from the ViewModel. Table of Contents xmlns:app="https://schemas.android.com/apk/res-auto"> <​data> <variable The ProgressBar would be displayed to simulate the login feature. Currently i've made integer LiveData that shows downloading progress, and subscribing viewholders to it in onBindViewHolder method of adapter, but i'm not sure is this right way, because i heve to expose my data everywhere (in viewmodel class and adapter).

Comments
  • post code where you are displaying ProgressDialog and hiding it
  • Have updated the answer ...dont mind the edit please @Akhil
  • 1st time the dialog dismissed correctly* but as the screen rotates the dialog keeps coming ... (as onChanged is called only once)..
  • I have used fragment and the fragment is reused Once the data is loaded hasData() is always true 2nd time if the fragment loads dialog wont show...
  • Did u get my point ?? 2nd time if the data is different onChanged will be called but progress wont show before..
  • Its showing a blank screen if the data is changed and after the response comes and data is loaded the progress dialog shows for some time...
  • i just want to avoid the user from seeing the blank screen...before any data loads...
  • You should use first code snippet in your onCreate as well... i.e. if no data then show ProgressDialog in onCreate also
  • i want to recreate my activity on rotation... keeping that condition i want some callback from liveData where i can dismiss the dialog
  • you absolutely shouldn't do that
  • Dont do it . Its absolutely wrong