in FragmentA() I get data from the previously Fragment
class FragmentA():Fragment() {
private lateinit var personList: MutableList<Person>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
personList = it.getParcelableArrayList<Person>("person") as MutableList<Person>
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if(personList.isEmpty()){
showEmptyContainer()
}else{
recyclerAdapter.setItems(personList)
}
}
Now, this code works when I open FragmentA(), but now, if I go from this fragment to FragmentB() and come back, my data is duplicated. So, I tried cleaning the array and set it up again
class FragmentA():Fragment() {
private lateinit var personList: MutableList<Person>
private var backupList:MutableList<Person> = mutableListOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
personList = it.getParcelableArrayList<Person>("person") as MutableList<Person>
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if(personList.isEmpty()){
showEmptyContainer()
}else{
backupList.clear()
backupList = personList
recyclerAdapter.setItems(backupList)
}
}
Doing this, it works when coming back, but, for some reason it shows the empty container when I do this twice so my question is
How can I retain this fragment personList while navigation forward and pressing the back button ?
Thanks