问题
So i have a RegisterActivity
and a MapsActivity
.
When a new user opens the app, they will be on the RegisterActivity
and sign up. Once they sign up, my app creates an Intent
and sends them to the MapsActivity
The problem is whenever the user closes the app, they have to login/register again.
I want the the app to be able to open back to the MapsActivity
since the user has not signed out yet
Here's my attempt:
i put an <intent-filter>
in both RegisterActivity
and MapsActivity
because i think both should both be the main launchers depending if a user is logged in or not right?
- If they aren't logged in, then the main launcher should be
RegisterActivity
- If they are logged in, then the main launcher should be
MapsActivity
AndroidManifest.xml
<application
<activity android:name=".MapsActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".RegisterActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".LoginActivity">
</activity>
</application>
MapsActivity.kt
public override fun onStart() {
super.onStart()
// Check if user is signed in (non-null) and update UI accordingly.
val currentUser = FirebaseAuth.getInstance().currentUser
if(currentUser == null){
Log.d(TAG, "User is not logged in")
val intent = Intent(this, LoginActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}else {
Log.d(TAG, "User is logged in")
}
}
回答1:
The simplest way to achieve this is to use a listener. Let's assume you have two activities, the LoginActivity
and the MainActivity
. The listener that can be created in the LoginActivity
should look like this:
val authStateListener = FirebaseAuth.AuthStateListener { firebaseAuth ->
val firebaseUser = firebaseAuth.currentUser
if (firebaseUser != null) {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}
}
This basically means that if the user is logged in, skip the LoginActivity
and go to the MainActivity
.
Instantiate the FirebaseAuth
object:
val firebaseAuth = FirebaseAuth.getInstance()
And start listening for changes in your onStart()
method like this:
override fun onStart() {
super.onStart()
firebaseAuth!!.addAuthStateListener(this.authStateListener!!)
}
In the MainActivity
, you should do the same thing:
val authStateListener = FirebaseAuth.AuthStateListener { firebaseAuth ->
val firebaseUser = firebaseAuth.currentUser
if (firebaseUser == null) {
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
}
}
Which basically means that if the user is not logged in, skip the MainActivity
and go to the LoginActivity
. In this activity you should do the same thing as in the LoginActivity
, you should start listening for changes in the onStart()
.
In both activities, don't forget to remove the listener in the moment in which is not needed anymore. So add the following line of code in your onStop()
method:
override fun onStop() {
super.onStop()
firebaseAuth!!.removeAuthStateListener(this.authStateListener!!)
}
回答2:
I have developer a similar app and I simply use a splash screen that checks if the users is logged or not and then open the right screen. The splash doesn't need to be to complex: in fact you can even avoid to set a view and just check whatever you want. This article explains it very clearly.
Remember to call finish() on the splash activity after launching the right one.
来源:https://stackoverflow.com/questions/54046250/android-studio-kotlin-user-has-to-log-back-into-app-every-time-app-is-closed