CoderUni
CoderUni

Reputation: 6134

Recyclerview items become invisible when switching fragments

After figuring out how to use a Recyclerview to display items, I've encountered a bug wherein the Recyclerview's items disappears when I switch between fragments.

Solution: It turns out that the Main Activity Xml gets replaced by the fragment then when I come back it inflates a blank fragment. All I had to do was to create a different fragment for the Main Activity.

Upvotes: 0

Views: 773

Answers (1)

Mr. Patel
Mr. Patel

Reputation: 1385

To solve this go to your class where you're passing the arraylist of data to recyclerview adapter and simply put that arraylist into an companion object such that whenever it's recreated it would retain it's previous value eg: Put this inside your class:-

 fun loaddataintoarrlist(){
    //load your data into arraylist such that it's not empty
arrlist.add("abc")
    }

companion object{
//Your arraylist
var arrlist:Arraylist?=null
}

Upvotes: 2

Related Questions