Android Studio(Kotlin)-每次关闭应用程序时,用户都必须重新登录到应用程序 [英] Android Studio (Kotlin) - User has to log back into app every time app is closed

查看:766
本文介绍了Android Studio(Kotlin)-每次关闭应用程序时,用户都必须重新登录到应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个RegisterActivityMapsActivity. 当新用户打开应用程序时,他们将位于RegisterActivity上并进行注册.他们注册后,我的应用程序会创建一个Intent并将其发送到MapsActivity

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.

我希望该应用能够重新打开MapsActivity,因为用户尚未退出

I want the the app to be able to open back to the MapsActivity since the user has not signed out yet

这是我的尝试:

我在RegisterActivityMapsActivity中都放置了<intent-filter>,因为我认为这两个都应该是主要的启动器,具体取决于用户是否登录?

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?

  • 如果未登录,则主启动器应为RegisterActivity
  • 如果已登录,则主启动器应为MapsActivity
  • 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")
    }
}

推荐答案

最简单的方法是使用侦听器.假设您有两个活动,LoginActivityMainActivity.可以在LoginActivity中创建的侦听器应如下所示:

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()
    }
}

这基本上意味着,如果用户已登录,请跳过LoginActivity并转到MainActivity.

This basically means that if the user is logged in, skip the LoginActivity and go to the MainActivity.

实例化FirebaseAuth对象:

val firebaseAuth = FirebaseAuth.getInstance()

然后开始监听onStart()方法中的更改,如下所示:

And start listening for changes in your onStart() method like this:

override fun onStart() {
    super.onStart()
    firebaseAuth!!.addAuthStateListener(this.authStateListener!!)
}

MainActivity中,您应该执行相同的操作:

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)
    }
}

这基本上意味着,如果用户未登录,请跳过MainActivity并转至LoginActivity.在此活动中,您应该执行与LoginActivity中相同的操作,应该开始侦听onStart()中的更改.

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().

在这两个活动中,不要忘记在不再需要时删除监听器.因此,在您的onStop()方法中添加以下代码行:

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!!)
}

这篇关于Android Studio(Kotlin)-每次关闭应用程序时,用户都必须重新登录到应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆