如何在我的ViewModel类中传递活动上下文?

huangapple go评论58阅读模式
英文:

How to pass activity context in my ViewModel class?

问题

class LoginViewModel: ViewModel() {
    var auth: FirebaseAuth = FirebaseAuth.getInstance()

    fun signIn(email: String, password: String, context: Context) {
        auth.signInWithEmailAndPassword(email, password)
            .addOnCompleteListener(context) { task ->

            }
        }
    } 
英文:

I am making an app with firebase authentication that uses signInWithEmailAndPassword() to authenticate the users. Now I also need the activity context with signInWithEmailAndPassword(). So, how do I pass the activity context in my viewmodel class?

class LoginViewModel: ViewModel() {
var auth: FirebaseAuth = FirebaseAuth.getInstance()

fun signIn(email: String, password: String) {
    auth.signInWithEmailAndPassword(email, password)
        .addOnCompleteListener(..context required..) { task ->

        }
    }
} 

I have already gone through almost all questions related to my query and everyone in the answers come to the conclusion that using ActivityViewModel() instead of ViewModel() and getting application context is the way to go. But as I understand ApplicationContext won’t respond to configuration changes, so I don't think this is the way to go. The most popular question on SO with the same query is this. But they also said to use application context. So I thought of using a dependency injection framework like Dagger/HILT to inject the context that I need. Now my question is, will the DI framework take care of the activity context once the activity is destroyed and avoid any memory leaks?

答案1

得分: 1

Ideally, your viewmodel does not have a reference to its hosting activity, as that invites memory leaks and configuration change problems.

Also, bear in mind that the sign-in flow involves navigation to an externally-supplied UI. Usually, navigation is managed by the UI layer (activity, fragment, or composable), perhaps acting on instructions from a viewmodel.

So, I would have your signInWithEmailAndPassword() call and the addOnCompleteListener() call be made in the UI layer. The on-complete listener could call a function on the viewmodel to inform it of the successful result, if desired.

英文:

Ideally, your viewmodel does not have a reference to its hosting activity, as that invites memory leaks and configuration change problems.

Also, bear in mind that the sign-in flow involves navigation to an externally-supplied UI. Usually, navigation is managed by the UI layer (activity, fragment, or composable), perhaps acting on instructions from a viewmodel.

So, I would have your signInWithEmailAndPassword() call and the addOnCompleteListener() call be made in the UI layer. The on-complete listener could call a function on the viewmodel to inform it of the successful result, if desired.

答案2

得分: 0

I think you can use @ActivityContext same as @ApplicationContext. Both are provided by Hilt by default, so you don't need to do much work on it.

class LoginViewModel: ViewModel() {
var auth: FirebaseAuth = FirebaseAuth.getInstance()

@ActivityContext
var context: Context

fun signIn(email: String, password: String) {
    auth.signInWithEmailAndPassword(email, password)
        .addOnCompleteListener(..上下文所需..) { task ->

        }
    }
}

For more information, you can check this point:

https://developer.android.com/training/dependency-injection/hilt-android#component-default

英文:

I think you can use @ActivityContext same as @ApplicationContext. Both are provided by hilt bydefault so you don't need to do much work on it

class LoginViewModel: ViewModel() {
var auth: FirebaseAuth = FirebaseAuth.getInstance()

@ActivityContext 
var context: Context

fun signIn(email: String, password: String) {
    auth.signInWithEmailAndPassword(email, password)
        .addOnCompleteListener(..context required..) { task ->

        }
    }
}

For more information , you can check this point

https://developer.android.com/training/dependency-injection/hilt-android#component-default

huangapple
  • 本文由 发表于 2023年5月21日 10:24:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定