0

I have an Android App that allows users to create a user account, but also to continue without a user account. The Authentication is done with Firebase. The MainActivity is the Login-Activity. Here, the user can choose to login or to continue as guest (without user account). If they login, they can choose whether they want to stay logged in or not when they close the app. I want, that when they continue as guest or when they check the stay-logged-in-checkbox, they get directly navigated to the next activity and the LoginActivity gets destroyed with finish(), so that the next activity is the first in the Backstack. So, this is the Activity, where the app gets closed when the user presses the Back-Button. And in this case, I want to check if the user should get logged out or not. I have done this as follows:

In the MainActivity, I save a Boolean-Variable in the SharedPreferences:

override fun login(email: String, password: String, stayLoggedIn: Boolean) {
    mAuth.signInWithEmailAndPassword(email, password)
        .addOnCompleteListener(this) { task ->
            if(task.isSuccessful) {
                getDefaultSharedPreferences(this).edit().putBoolean(getString(R.string.continue_as_guest), false).apply()
                getDefaultSharedPreferences(this).edit().putBoolean(getString(R.string.stay_logged_in), stayLoggedIn).apply()
                startActivity(Intent(this, NavigationActivity::class.java))
                finish()
            } else {
                //show message
            }
        }
}

Then, in the next Activity, which is now the first Activity in the backstack, I override the onDestroy Method:

override fun onDestroy() {
    super.onDestroy()
    val stayLoggedIn = getDefaultSharedPreferences(this).getBoolean(getString(R.string.stay_logged_in), false)
    if(!stayLoggedIn && mAuth.currentUser != null) {
        mAuth.signOut()
    }
}

This works great, when the user coles the app the usual way through the Back-Button. But when the app gets killed in another way (phone turns off because battery is empty or app gets killes through home button), the onDestroy-Method doesn't get called, which means the user doesn't get signed out.

I have read that you can't handle those cases inthe process where the app is killed where the onDestroy-Method is not called, so you have to handle it when the app is opened again. But I have no idea how that works. Hope someone can help me.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Apri
  • 1,241
  • 1
  • 8
  • 33
  • You should be able to call multiple `put*` methods on the same instance of a `SharedPreferences.Editor` - for example: `sharedPrefs.edit().putBoolean("key_goes_here", true).putBoolean("another_key", false)` instead of getting separated instances of the editor. – Edric Sep 11 '19 at 11:00

1 Answers1

2

When you install application first time following method call one by one in Activity

onCreate()
onStart()
onResume()

after that when you press Home Button then following method call

onPause()
onStop()

Note: onDestroy() method not call after press Home Button.

And regarding low battery please follow this link

Mohan Sai Manthri
  • 2,808
  • 1
  • 11
  • 26