Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get activity context in ViewModel Android?

I am using MVVM. I want to implement Firebase Auth in app. But to implement it I need an activity context in my repo class. How can I get it from ViewModel or is there any simple method available?

Here is the firebase code I need to implement:

 PhoneAuthProvider.getInstance().verifyPhoneNumber("+91"+phone,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            (Activity) context,               // Activity (for callback binding)
            new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
                @Override
                public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) 
               {
                    signInWithPhoneAuthCredential((Activity)context,phoneAuthCredential);
                }

                @Override
                public void onVerificationFailed(@NonNull FirebaseException e) {
                    setLoginFailed(e);

                }

                @Override
                public void onCodeSent(@NonNull String s, @NonNull 
                PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                    super.onCodeSent(s, forceResendingToken);
                    loginResponse.setOnProgress(false);
                    loginResponse.setStatus(true);
                    loginResponse.setCodeVerified(false);
                    loginResponseLiveData.setValue(loginResponse);
                    verificationId =s;

                }
            });
like image 293
sum20156 Avatar asked Mar 07 '26 06:03

sum20156


2 Answers

In general, ViewModel objects are not supposed to reference anything that has to do with the Android platform APIs, especially Activity objects. You would not want a ViewModel to retain (and leak) an Activity across orientation changes. Retaining Activity objects in a ViewModel is a huge anti-pattern that should be avoided.

Instead, you should use a different version of that Firebase API. Choose one of the alternatives that do not take an Activity, according to the API documentation. Once the API is complete, you can bubble a callback up to the hosting activity to start any other activities.

like image 182
Doug Stevenson Avatar answered Mar 10 '26 01:03

Doug Stevenson


First of all, according to official view model guidelines:

Caution: A ViewModel must never reference a view, Lifecycle, or any class that may hold a reference to the activity content

So in accordance with your MVVM architecture, don't pass your activity context to a ViewModel.

I guess in that method you want to implement you do not specifically need an activity context. You could also use the context of your application.

Follow these 3 steps of this answer to get access your Application context statically:

  1. create Application class which references the context on create
    public class MyApplication extends Application {
    
        private static Context context;
    
        public void onCreate() {
            super.onCreate();
            MyApplication.context = getApplicationContext();
        }
    
        public static Context getAppContext() {
            return MyApplication.context;
        }
    }
    
  2. Declare your application class in your Manifest
    <application android:name="com.xyz.MyApplication">
    
    </application>
    
  3. Access this context statically inside your repository class
    MyApplication.getAppContext()
    

There is also the possibility to access the application context via AndroidViewModel class. But I guess you usually do not want to initialize your repository from your ViewModel.

like image 23
cewaphi Avatar answered Mar 09 '26 23:03

cewaphi