Search code examples
android-intentandroid-activityfirebase-authenticationfirebase-dynamic-links

How to send the result of Firebase Sign In with Email Link to my app's main Activity


I'm trying to implement Firebase SIgn In with Email Link in a Jetpack Compose app. When the user clicks on the verification link in the email it activates a Firebase Dynamic Link, which launches a second activity in my app, where I can check that the verification was successful. But how can I get this second activity to signal the first one (which is waiting in a suspended coroutine) that the process has completed?

I can only find documentation and examples for two ways of passing data between activities, neither of which apply here. Usually you would call startActivityForResult (or use rememberLauncherForActivityResult in a composable context) where the main activity constructs an intent which provides the second intent with a way of passing its result back to the activity that launches it, and this is how Sign In With Google etc work. But there doesn't seem to be a way to achive that with the Dynamic Links mechanism used by Firebase Sign In with Email Link.


Solution

  • You can either use broadcast receiver in your first activity and send broadcast there from the second one:

    # Activity 1
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter("INTENT_NAME"));
    }
    
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String receivedLoginStatus = intent.getBooleanExtra(LOGIN_RESULT);
        }
    };
    
    
    # Activity 2
    
    Intent intent = new Intent("INTENT_NAME").putExtra(LOGIN_RESULT, true);
    LocalBroadcastManager.getInstance(Activity2.this).sendBroadcast(intent);
    

    Or you can just update userInfo in Datastore (SharedPreferences) and listen for the changes in your first activity.