Bartos
Bartos

Reputation: 1037

Restore RecyclerView state after rotating

I've got RecyclerView with list of Users. After clicking specific row I display dialog and user can change cell background color. Everything works , however , when I rotate mobile it comes back to white background. How can I save state of recyclerView and restore it after rotate ? I tried with saveInstanceState but couldn't archieve that effect.

MyViewHolder in Adapter :

public MyViewHolder(View view) {
                super(view);
                user_name = (TextView) view.findViewById(R.id.user_name);
                user_surname = (TextView) view.findViewById(R.id.user_surname);
                User_description = (TextView) view.findViewById(R.id.user_description);
                user_dob = (TextView) view.findViewById(R.id.user_dob);
                user_avatar = (AvatarImageView) view.findViewById(R.id.user_avatar);
                mTrash = (ImageView) view.findViewById(R.id.trash_img);


                item_ll = (LinearLayout)view.findViewById(R.id.item_layout);
                item_ll.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        ((MainActivity) ctx).userItemClick(getAdapterPosition());

                    }
                });
            }

MainActivity has userItemClick method :

 @Override
        public void userItemClick(final int pos) {
            Log.e(TAG, "userItemClick: " + pos );
            DetailsDialog dialog = new DetailsDialog(this,mRecyclerView,pos);
            dialog.show();
        }

And here's my custom Dialog class :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_details);
    mYelloewLayout = (LinearLayout) findViewById(R.id.yellow_layout);
    mGreenLayout = (LinearLayout) findViewById(R.id.green_layout);
    mRedLayout = (LinearLayout) findViewById(R.id.red_layout);
    mYelloewLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            view.getChildAt(pos).setBackgroundColor(getContext().getResources().getColor(R.color.yellow));
            dismiss();
        }
    });
    mRedLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            view.getChildAt(pos).setBackgroundColor(getContext().getResources().getColor(R.color.red));
            dismiss();
        }
    });
    mGreenLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            view.getChildAt(pos).setBackgroundColor(getContext().getResources().getColor(R.color.green));
            dismiss();
        }
    });


}

here is my Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.bartoszszlapa.api_request">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:replace="android:icon"
    android:name="com.orm.SugarApp"
    android:configChanges="orientation|screenSize"
    >
    <meta-data android:name="DATABASE" android:value="sugar_example.db" />
    <meta-data android:name="VERSION" android:value="2" />
    <meta-data android:name="QUERY_LOG" android:value="true" />
    <meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example" />
    <activity android:name=".views.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Any help, please ? :)

Upvotes: 1

Views: 930

Answers (1)

Abdul Ahad
Abdul Ahad

Reputation: 540

You will just have to add this line

<activity name= ".YourActivity" android:configChanges="orientation|screenSize"/>

To the activity which has the RecyclerView.

Upvotes: 3

Related Questions