I keep getting java.lang.IllegalArgumentException: snippets or attached views cannot be recycled. isScrap: false isAttached: true when using Fragment with RecyclerView. I have only 1 activity that switches between multiple fragments. In the onCreate action, I set the default fragment, which, as it turned out, has a RecyclerView, implemented exactly the same as in the documentation. When the activity starts, I get java.lang.IllegalArgumentException: snippets or attached views cannot be processed. isScrap: false isAttached: true .
The problem is that at the beginning I load an empty container with fragments and then iterate over the fragment using RecyclerView, it works fine. Also, I do not use android: animateLayoutChanges or notifyDataSetChanged () , as indicated here . Currently I am setting up a RecyclerView in the fragment's onResume () method. I tried switching it to other life cycle methods, but not luck.
Any help is appreciated.
thanks
I added only the relevant code snippets. I believe this is due to some kind of life cycle, given the fact that it works if I do not set the fragment in onCreate () Activity. It worked when I had a ListView instead of a RecyclerView. I did not post the code for RecyclerView, because it is the same as in the documentation.
OnCreate action
public void onCreate(Bundle savedInstanceState) { Log.d(TAG,"### onCreate ###"); super.onCreate(savedInstanceState); setContentView(R.layout.efficientix_activity_layout); if(savedInstanceState != null){ checkedSideMenuItemLabel = savedInstanceState.getInt("checkedSideMenuItemLabel"); } //Init components initActionBar(checkedSideMenuItemLabel,savedInstanceState); initSideMenuArrayAdapter(); initSideMenu(checkedSideMenuItemLabel,savedInstanceState); initActionBarDrawerToggle(); fragmentManager = this.getSupportFragmentManager(); if(savedInstanceState == null){ //Set default fragment upon activity creation. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); DashboardFragment df = new DashboardFragment(); fragmentTransaction.replace(R.id.fragment_container,df,DashboardFragment.class.toString()); fragmentTransaction.commit(); } }
Fragment onResume ()
public void onResume() { super.onResume(); if (recylerViewLayoutManager == null) { recylerViewLayoutManager = new LinearLayoutManager(this.getActivity()); } recylerView.setLayoutManager(remindersLayoutManager); initRecylerViewAdapter(); recylerView.setAdapter(recylerViewAdapter); }
Fragment Layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/myRecyclerView" style="@style/Main_Content_List_View"/> </LinearLayout>
Action layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:efficientix="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/actionbar_toolbar"/> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/menu_layout" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/fragment_container" android:orientation="vertical"> </LinearLayout> <include layout="@layout/side_menu_layout"/> </android.support.v4.widget.DrawerLayout> </LinearLayout>
RecyclerView Element Layout
<?xml version="1.0" encoding="utf-8"?> <android.widget.TableLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/Main_Content_Table_Layout_List_Item"> <TableRow style="@style/Main_Content_Table_Row_List_Item"> <TextView android:id="@+id/description" style="@style/Main_Content_TextView" /> <ImageView android:id="@+id/category" style="@style/Main_Content_ImageView"/> </TableRow> <TableRow style="@style/Main_Content_Table_Row_List_Item"> <TextView android:id="@+id/alert_date" style="@style/Main_Content_TextView" /> <TextView android:id="@+id/type" style="@style/Main_Content_TextView" /> </TableRow> </android.widget.TableLayout>