Search code examples
androidfirebase-authenticationsharedpreferences

How to use single activity for different users at the same time with firebase


I wanna know that how can i retrieve different user information in a single activity. In my case i have a ProfileActivity and i want that when i click on the profile button it retrieve my info from Firestore with my FirebaseAuth uid and when i search for another user and click on his profile it intent me the ProfileActivity so i did that , and it is working so fine, But the problem is that when i logout and login again my old information appears and when i click back button and after come back to the ProfileActivity now it show me my information, so i do not understand that why my info not shows me just after login.

private  lateinit var profileId: String  //initialization...

in onCreate...

firebaseUser= FirebaseAuth.getInstance().currentUser!!
        val pref = getSharedPreferences("PREF", Context.MODE_PRIVATE)
        if (pref != null)
        {
            this.profileId = pref.getString("profileId", "none").toString()
        }

onStop...

override fun onStop() {
        super.onStop()
        val pref = getSharedPreferences("PREF", Context.MODE_PRIVATE)?.edit()
        pref?.putString("profileId", firebaseUser.uid)
        pref?.apply()
    }

method to retrieve info as userInfo called in oncreate

private fun userInfo() {
        val userRef = FirebaseFirestore.getInstance().collection("Users").document(profileId)
        userRef.get()
                .addOnSuccessListener { documentSnapshot ->
                    if (documentSnapshot != null && documentSnapshot.exists()) {
                       val user = documentSnapshot.toObject(User::class.java)
                        profile_fullname?.text = user.getFullname()
                        profile_about?.text = user.getAbout() 
                    } 
           }
 }

And this is how i intent from adapter to other user (profile activity).

 holder.itemView.setOnClickListener {
           val pref = mContext.getSharedPreferences("PREF", Context.MODE_PRIVATE).edit()
               pref.putString("profileId", user.getUid())
               pref.apply()
           val intent = Intent(mContext, ProfileActivity::class.java )
            mContext.startActivity(intent)
    }

And this is logout operation

logoutButton.setOnClickListener {
            FirebaseAuth.getInstance().signOut()
            val intent = Intent(this@EditProfileActivity, LoginActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
            startActivity(intent)
            finish()
        }

Solution

  • You just have to share the preference value at the time of going to the profile activity like this

    goToProfileActivityIcon.setOnClickListener {
               val pref = mContext.getSharedPreferences("PREF", Context.MODE_PRIVATE).edit()
                   pref.putString("profileId", currentUserId)
                   pref.apply()
               val intent = Intent(mContext, ProfileActivity::class.java )
                mContext.startActivity(intent)
        }
    

    Where the goToProfileActivityIcon is the button which you are using to go switch to profile activity.