Reputation: 371
While testing my application on a real device, it works fine onStart, but onPause application to open another one, the application doesn't react well on click to resume the application again, in the screenshot below . It is late for 1 second and starts again. Then, the RecyclerView in fragment reloads everytime i start the application. If anyone can help, thank you.
ChatListFragment.kt
class ChatListFragment : Fragment() {
private lateinit var binding: FragmentChatListBinding
private var chatList = ArrayList<Chats>()
private var user: Chats?= null
private var chatListAdapter = ChatListAdapter(chatList)
private lateinit var auth: FirebaseAuth
private lateinit var storage: FirebaseStorage
private lateinit var firestore: FirebaseFirestore
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_chat_list, container, false)
binding = FragmentChatListBinding.inflate(layoutInflater)
init()
addContacts()
changeListener()
// Calling the override functions from
// the Linear Layout Manager inner class
val myLinearLayoutManager = object : LinearLayoutManager(context) {
override fun canScrollVertically(): Boolean {
return false
}
}
view.popChatsRec.layoutManager = myLinearLayoutManager
view.popChatsRec.adapter = chatListAdapter
chatList.clear()
return view
}
private fun addContacts(){
val firebaseUser = auth.currentUser
if (firebaseUser != null){
firestore.collection("Contacts").document(auth.currentUser?.uid!!).collection("rooms").get()
.addOnSuccessListener {
if (!it.isEmpty){
for (documents in it.documents){
val uId = documents.data?.get("uId").toString()
val uName = documents.data?.get("fullName").toString()
val uNum = documents.data?.get("fullNum").toString()
val uStatus = documents.data?.get("status").toString()
val uPresence = documents.data?.get("presence").toString()
val uImg = documents.data?.get("image").toString()
chatList.add(Chats(uId, uImg, uName, "", uNum, uStatus, uPresence))
}
} else {
Toast.makeText(context, "FailedHere", Toast.LENGTH_SHORT).show()
}
chatListAdapter.notifyDataSetChanged()
}.addOnFailureListener { Toast.makeText(context, "FailureOverall", Toast.LENGTH_SHORT).show() }
} else{
Toast.makeText(context, "Null", Toast.LENGTH_SHORT).show()
}
}
private fun changeListener(){
firestore.collection("Contacts").document(auth.currentUser?.uid!!).collection("rooms")
.addSnapshotListener { snapshots, e->
if (e != null){
Log.w(ContentValues.TAG, "listen:error", e)
return@addSnapshotListener
}
for (dc in snapshots!!.documentChanges) {
if (dc.type == DocumentChange.Type.MODIFIED) {
chatList.clear()
addContacts()
Log.d(ContentValues.TAG, "New city: ${dc.document.data}")
}
}
}
}
private fun init(){
auth = FirebaseAuth.getInstance()
storage = FirebaseStorage.getInstance()
firestore = Firebase.firestore
}
}
Upvotes: 1
Views: 96