Skip to content Skip to sidebar Skip to footer

Android Refreshing Adapter Work After Rotation Again Device

This code works fine when i add some data into 'List model' and restore saved data on rotation device, unfortunately after restore data and set that to model within onRestoreInstan

Solution 1:

My problem resolved,

after adding this below line into manifest for specific activity where you setAdapter

android:configChanges="screenSize|orientation|screenLayout"

But it will return best view if you design Both Portrait and Landscape layout or running app only in one mode of these both.

Solution 2:

The main reason is that, because you called set-data and that method did not immediately notify that the dataset was changed, therefore it was not ware.

      public void setData(List<RobotViewModel> data) {
            Log.e("data size ", data.size() + "");
            list.clear();
            list.addAll(data);
        }
publicvoidsetData(List<RobotViewModel> data) {
            Log.e("data size ", data.size() + "");
            list.clear();
            notifyDataSetChanged(); //here,to signal change occurred.
            list.addAll(data);
            notifyDataSetChanged(); //here,to signal change occurred.

        }

should probably get into the habit of notifying changes on the spot.

The main problem is this, you never notified the changes you made, you cleared it , but you never told it, you know, bu the adapter does not know, also you inserted fresh data with addAll but you still did not notify it.

Solution 3:

I tried your code and it works just fine with few updates. When you perform this action

 model.add(temp);
 adapter.notifyItemInserted(model.size() - 1);

You are assuming the persistence of the link between the object model and the adapter datasource. This assumption is wrong, in fact if you just replace the code above with

 model.add(temp);
 adapter.setData(model);
 adapter.notifyItemInserted(model.size() - 1);

it works fine. I hope it helped.

Post a Comment for "Android Refreshing Adapter Work After Rotation Again Device"