I am trying to add a fragment to the start of a FragmentStateAdapter and it keeps throwing the IllegalStateException error saying that the fragment has already been added, however if I use the exact same code except that instead of adding the fragment at the start I add it at the end it gets added to the end perfectly fine! Of course it is however in the completely wrong place. Here below is my code:
class PostAdapter(fragmentModalList: ArrayList<PostModal>,
private val activity: PostScreenFragmented
): FragmentStateAdapter(activity) {
private val fragmentList: ArrayList<PostFragment> = ArrayList()
init {
for (modal in fragmentModalList) {
fragmentList.add(PostFragment(this, modal))
}
}
override fun getItemCount(): Int {
if (fragmentList.isNotEmpty() && fragmentList[fragmentList.size-1].modal.sentTime != null) {
return fragmentList.size + 1
}
return fragmentList.size // TODO make this work for posts before the first
}
override fun createFragment(position: Int): Fragment {
if (position < fragmentList.size && position >= 0) {
val frag = fragmentList[position]
return frag
} else if (fragmentList.isNotEmpty() && position == fragmentList.size) { // create a new fragment with only prevSentTime
val newModal = PostModal(fragmentList[fragmentList.size-1].modal.sentTime, true)
val frag = PostFragment(this, newModal)
fragmentList.add(frag)
return frag
} else {
TODO() // this should never happen, it is here purely so that we don't get a compile error
}
}
fun addFragmentAtStart(newModal: PostModal) {
// Ensure the new modal is unique based on your criteria
if (fragmentList.none { it.modal == newModal }) {
// Create a new fragment with the provided modal
val newFragment = PostFragment(this, newModal)
// Add the new fragment at the start of the list
fragmentList.add(0, newFragment) // Insert at the start
// Notify the adapter of the insertion
notifyItemInserted(0) // Notify the position where the fragment was added
notifyItemRangeChanged(1, fragmentList.size) // Update subsequent items
} else {
// Handle the case where the fragment already exists
Log.d("Adapter", "Fragment with the same modal already exists.")
}
}
}
when this adapter is initialised it is initialized with 2 modals and started at index 1, when scrolling to the fragment at index 0 it will start fetching the relevant data from the database and once that is done it will call adapter.addFragmentAtStart(newModal)
using a newModal with the data needed for the next fragment to fetch the necessary data from the database again. The problem is that instead of doing this I get an error, here is the stacktrace:
FATAL EXCEPTION: main
Process: 'packageName', PID: 4192
java.lang.IllegalStateException: Fragment already added
at androidx.fragment.app.Fragment.setInitialSavedState(Fragment.java:831)
at androidx.viewpager2.adapter.FragmentStateAdapter.ensureFragment(FragmentStateAdapter.java:269)
at androidx.viewpager2.adapter.FragmentStateAdapter.onBindViewHolder(FragmentStateAdapter.java:175)
at androidx.viewpager2.adapter.FragmentStateAdapter.onBindViewHolder(FragmentStateAdapter.java:67)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7254)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7337)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6194)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6460)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4309)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4012)
at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:2028)
at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5434)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1406)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1415)
at android.view.Choreographer.doCallbacks(Choreographer.java:1015)
at android.view.Choreographer.doFrame(Choreographer.java:941)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1389)
at android.os.Handler.handleCallback(Handler.java:959)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loopOnce(Looper.java:232)
at android.os.Looper.loop(Looper.java:317)
at android.app.ActivityThread.main(ActivityThread.java:8705)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
I am quite confident I am overlooking something which is making it so that I get this error instead of it working properly (like I already said, the correct posts do load if I make them load at the end instead of the beginning). What could be causing this error? If anything else is needed please let me know.