2

I enabled the skip login, wherein I am not able to get the email of the account I was logged in to.

How can I still get the email if skip login is enabled in the second launch of the app?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 2
    Can you clarify what you mean by "f skip login is enabled"? The relevant code, or a screenshot of the relevant UI, would probably help for that. – Frank van Puffelen Nov 15 '22 at 02:45
  • On first launch you will locally save login credentials of user, And at second time you will have that data and able to fetch user information. Check SharedPrefrences https://developer.android.com/reference/android/content/SharedPreferences for saving data locally. Hope it helps – Syed Ibrahim Nov 15 '22 at 04:06
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Nov 15 '22 at 04:43
  • thank you for responding, I have solved the problem by adding sharedpreferences – Jason Junio Nov 15 '22 at 12:45

2 Answers2

1

The Firebase authentication state persists across application restarts. So there is no need to save the email address locally in order to use it again when you restart the application. As soon as you create an instance of the FirebaseAuth class, you can call getCurrentUser() which:

Returns the currently signed-in FirebaseUser or null if there is none.

As you can see, the type object that is returned is FirebaseUser. If this object is null it means that your user is not logged in, otherwise, you can call FirebaseUser#getEmail(). In this way, you can always get the email address from this object, without the need to perform any other operations.

If you want to track the auth state, then I recommend you check my answer from the following post:

If you want to see a real example, please check this resource, with the corresponding repo.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

While launching the app first time, after login you can save the username and email in Shared Preference and on second time launch you can fetch it. First initialize App Preference using method

AppPreferences.INSTANCE.initAppPreferences(ActivityName.this);

Then set userName and use it.

Code is shared below

public enum AppPreferences {
INSTANCE;
private static final String SHARED_PREFERENCE_NAME = "AppName";
private SharedPreferences mPreferences;
private Editor mEditor;


public void initAppPreferences(Context context) {
    mPreferences = context.getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
    mEditor = mPreferences.edit();
}

public String getUserName() {
    return mPreferences.getString(SharedPreferencesKeys.user_name.toString(), "");
}

public void setUserName(String value) {
    mEditor.putString(SharedPreferencesKeys.user_name.toString(), value);
    mEditor.commit();
}
}
Priyanka Singhal
  • 243
  • 1
  • 4
  • 20