I have a ViewPager
and TabLayout
setup with 4 fragments, like any modern social app. I've tried, tried, tried, but I couldn't find an answer to my problem. Within one of the tabs, I want to navigate from the fragment to another fragment but instead of navigating, it just puts it on top and I can still interact with the previous fragment. It's not replacing, it's simply just layering it on top.
Code:
// Chat fragment : Inside the onCreateView fun
this.loadConvos({
chats ->
this.chatsArray = chats
this.chatsArray.sortBy { it.timestamp}
this.chatsArray.reverse()
listView.adapter = ChatBaseAdapter(this.chatsArray, context)
listView.setOnItemClickListener {
parent, view, position, id ->
this.chatID = this.chatsArray[position].chatID!!
Toast.makeText(context, "Position Clicked: " + position, Toast.LENGTH_SHORT).show()
childFragmentManager
.beginTransaction()
.replace(R.id.chatFragmentLayout, MessagesFragment())
.addToBackStack(null)
.commit()
}
}, {
error ->
print(error)
})
The function simply just loads the listView
with chat details the user has which works and I can tap and the Toast will give me the cell position but when it commit()
s it just layers the MessagesFragment()
on top of the ChatsFragment()
. I'd also like to know how to pass information to the next fragment. This method I don't see a way to pass data, unlike the regular Bundle/Intent
way I know of.
XML Chat on the viewpager:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/chatFragmentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="io.jedi.jedi.fragments.ChatFragment">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
XML Messages
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/messagesFragmentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="io.jedi.jedi.fragments.MessagesFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Messages" />
</FrameLayout>