In this app, I have a logout button that's signed out the user if logged in with email & password or with a google account, the problem happens after signing out and re-launch the app again in the SplashActivity
, I do check for the currentUser
if is null
it should go to the login page, else it should go to the home page, but It seems the app remember the current user even it's logged out before, I tried this solution but it doesn't work
Gif describes the problem
the dagger hilt module
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Provides
@Singleton
fun provideFirebaseAuth() = FirebaseAuth.getInstance()
@Provides
@Singleton
fun provideFirebaseFirestore() = FirebaseFirestore.getInstance()
}
SplashActivity code
private const val TAG = "SplashActivity"
@AndroidEntryPoint
class SplashActivity : AppCompatActivity() {
private lateinit var binding: ActivitySplashBinding
@Inject
lateinit var firebaseAuth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySplashBinding.inflate(layoutInflater)
setContentView(binding.root)
val typeface = ResourcesCompat.getFont(this, R.font.blacklist)
val animation = AnimationUtils.loadAnimation(this, R.anim.my_anim)
binding.apply {
splashTextView.typeface = typeface
splashTextView.animation = animation
}
Log.d(TAG, "onCreate: ${firebaseAuth.currentUser?.providerData?.get(0)?.email}")
}
override fun onStart() {
super.onStart()
Handler(Looper.getMainLooper()).postDelayed({ /* Create an Intent that will start the Menu-Activity. */
if(firebaseAuth.currentUser == null) {
val intent = Intent(this@SplashActivity, MainActivity::class.java)
startActivity(intent)
this@SplashActivity.startActivity(intent)
this@SplashActivity.finish()
}else{
val intent = Intent(this@SplashActivity, HomeActivity::class.java)
startActivity(intent)
this@SplashActivity.startActivity(intent)
this@SplashActivity.finish()
}
}, 3000)
}
}
ProfileFragment "logout button code"
private const val TAG = "ProfileFragment"
@AndroidEntryPoint
class ProfileFragment : Fragment() {
private var _binding: FragmentProfileBinding? = null
private val binding get() = _binding!!
@Inject
lateinit var firebaseAuth: FirebaseAuth
private lateinit var googleSignInClient: GoogleSignInClient
private val viewModel by viewModels<CredentialsViewModel>()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
_binding = FragmentProfileBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.logoutButton.setOnClickListener {
val currentUser = firebaseAuth.currentUser ?: return@setOnClickListener
for (i in 0 until currentUser.providerData.size) {
when (currentUser.providerData[i].providerId) {
"google.com" -> {
//User signed in with a custom account
Log.d(
TAG,
"onViewCreated: provider is ${currentUser.providerData[i].providerId}"
)
googleSignInClient = GoogleSignIn.getClient(
requireActivity(),
viewModel.getGoogleSignInOptions()
)
googleSignInClient.signOut().addOnCompleteListener {
if (it.isSuccessful) {
redirectToMainActivity()
}else {
return@addOnCompleteListener
}
}
}
"password" -> {
Log.d(TAG, "onViewCreated: ${currentUser.providerData[i].providerId}")
firebaseAuth.signOut()
redirectToMainActivity()
}
else -> {
Log.d(TAG, "onViewCreated: ${currentUser.providerData[i].providerId}")
redirectToMainActivity()
return@setOnClickListener
}
}
}
}
}
private fun redirectToMainActivity() {
val intent = Intent(requireContext(), MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
requireActivity().finish()
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}