What is the correct way to use Firebase authentication?












0















I am just starting to work with FirebaseUI email/password authentication.



My app is very simple, so far it's made of 1 activity that holds 3 fragments using a TabLayout and looks like this:



public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
TabsAdapter adapter = new TabsAdapter(this, getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);

}
}


On the Firebase assistant, it shows that I need to check if the user is signed in, in the onStart:



@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}


Then, the assistant shows how to sign up and sign in a user, but it doesn't show in which part of the activity it should go: onCreate? onStart?



*This is the signing up code:



mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}

// ...
}
});


*This is the sign-in code



mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}

// ...
}
});


I am confused, assuming I currently have the working app, with the code provided above, what is the work flow now? Do I need to make this signup / signin code in the Mainactivity, and then, instead of the updateUI(null); pseudo-code provided by the Firebase assistant, I should make a new "main activity" with only the firebase auth code, and then use intents to send user to what's currently the main activity, which will be renamed to something like "SignedInActivity", or everything is done in the same current activity?



**Edit: To clarify what I mean:
I currently have MainActivity.
Do I need to:



1) change this current MainActivity to SignedInActivity, and then replace the current MainActivity with the Firebase activity, and then send intent to SignedInActivity upon successfull login



Or



2) Modify current MainActivity with the Firebase code so that I still have the current MainActivity with extra code of the Firebase?



I am confused as well, so hopefully my post is understandable










share|improve this question




















  • 1





    Getting everything right concerning the lifecycle is not trivial. If it's more than just playing around to get familiar with the Firebase API I recommend to use the FirebaseUI Auth library. It makes your life a lot easier.

    – Ewald Benes
    Nov 21 '18 at 11:56
















0















I am just starting to work with FirebaseUI email/password authentication.



My app is very simple, so far it's made of 1 activity that holds 3 fragments using a TabLayout and looks like this:



public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
TabsAdapter adapter = new TabsAdapter(this, getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);

}
}


On the Firebase assistant, it shows that I need to check if the user is signed in, in the onStart:



@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}


Then, the assistant shows how to sign up and sign in a user, but it doesn't show in which part of the activity it should go: onCreate? onStart?



*This is the signing up code:



mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}

// ...
}
});


*This is the sign-in code



mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}

// ...
}
});


I am confused, assuming I currently have the working app, with the code provided above, what is the work flow now? Do I need to make this signup / signin code in the Mainactivity, and then, instead of the updateUI(null); pseudo-code provided by the Firebase assistant, I should make a new "main activity" with only the firebase auth code, and then use intents to send user to what's currently the main activity, which will be renamed to something like "SignedInActivity", or everything is done in the same current activity?



**Edit: To clarify what I mean:
I currently have MainActivity.
Do I need to:



1) change this current MainActivity to SignedInActivity, and then replace the current MainActivity with the Firebase activity, and then send intent to SignedInActivity upon successfull login



Or



2) Modify current MainActivity with the Firebase code so that I still have the current MainActivity with extra code of the Firebase?



I am confused as well, so hopefully my post is understandable










share|improve this question




















  • 1





    Getting everything right concerning the lifecycle is not trivial. If it's more than just playing around to get familiar with the Firebase API I recommend to use the FirebaseUI Auth library. It makes your life a lot easier.

    – Ewald Benes
    Nov 21 '18 at 11:56














0












0








0








I am just starting to work with FirebaseUI email/password authentication.



My app is very simple, so far it's made of 1 activity that holds 3 fragments using a TabLayout and looks like this:



public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
TabsAdapter adapter = new TabsAdapter(this, getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);

}
}


On the Firebase assistant, it shows that I need to check if the user is signed in, in the onStart:



@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}


Then, the assistant shows how to sign up and sign in a user, but it doesn't show in which part of the activity it should go: onCreate? onStart?



*This is the signing up code:



mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}

// ...
}
});


*This is the sign-in code



mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}

// ...
}
});


I am confused, assuming I currently have the working app, with the code provided above, what is the work flow now? Do I need to make this signup / signin code in the Mainactivity, and then, instead of the updateUI(null); pseudo-code provided by the Firebase assistant, I should make a new "main activity" with only the firebase auth code, and then use intents to send user to what's currently the main activity, which will be renamed to something like "SignedInActivity", or everything is done in the same current activity?



**Edit: To clarify what I mean:
I currently have MainActivity.
Do I need to:



1) change this current MainActivity to SignedInActivity, and then replace the current MainActivity with the Firebase activity, and then send intent to SignedInActivity upon successfull login



Or



2) Modify current MainActivity with the Firebase code so that I still have the current MainActivity with extra code of the Firebase?



I am confused as well, so hopefully my post is understandable










share|improve this question
















I am just starting to work with FirebaseUI email/password authentication.



My app is very simple, so far it's made of 1 activity that holds 3 fragments using a TabLayout and looks like this:



public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
TabsAdapter adapter = new TabsAdapter(this, getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);

}
}


On the Firebase assistant, it shows that I need to check if the user is signed in, in the onStart:



@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}


Then, the assistant shows how to sign up and sign in a user, but it doesn't show in which part of the activity it should go: onCreate? onStart?



*This is the signing up code:



mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}

// ...
}
});


*This is the sign-in code



mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}

// ...
}
});


I am confused, assuming I currently have the working app, with the code provided above, what is the work flow now? Do I need to make this signup / signin code in the Mainactivity, and then, instead of the updateUI(null); pseudo-code provided by the Firebase assistant, I should make a new "main activity" with only the firebase auth code, and then use intents to send user to what's currently the main activity, which will be renamed to something like "SignedInActivity", or everything is done in the same current activity?



**Edit: To clarify what I mean:
I currently have MainActivity.
Do I need to:



1) change this current MainActivity to SignedInActivity, and then replace the current MainActivity with the Firebase activity, and then send intent to SignedInActivity upon successfull login



Or



2) Modify current MainActivity with the Firebase code so that I still have the current MainActivity with extra code of the Firebase?



I am confused as well, so hopefully my post is understandable







android firebase firebase-authentication firebaseui






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 14:52









Frank van Puffelen

229k28375399




229k28375399










asked Nov 21 '18 at 10:17









TTnoteTTnote

899




899








  • 1





    Getting everything right concerning the lifecycle is not trivial. If it's more than just playing around to get familiar with the Firebase API I recommend to use the FirebaseUI Auth library. It makes your life a lot easier.

    – Ewald Benes
    Nov 21 '18 at 11:56














  • 1





    Getting everything right concerning the lifecycle is not trivial. If it's more than just playing around to get familiar with the Firebase API I recommend to use the FirebaseUI Auth library. It makes your life a lot easier.

    – Ewald Benes
    Nov 21 '18 at 11:56








1




1





Getting everything right concerning the lifecycle is not trivial. If it's more than just playing around to get familiar with the Firebase API I recommend to use the FirebaseUI Auth library. It makes your life a lot easier.

– Ewald Benes
Nov 21 '18 at 11:56





Getting everything right concerning the lifecycle is not trivial. If it's more than just playing around to get familiar with the Firebase API I recommend to use the FirebaseUI Auth library. It makes your life a lot easier.

– Ewald Benes
Nov 21 '18 at 11:56












1 Answer
1






active

oldest

votes


















3














You really should look up the Android Lifecycle. It is important to know when the callbacks(onCreate, onStart, onResume, etc...) are called and then you can decide on your own where to implement the code.



In your case I would make a LoginActivity where you also can register a new profile with "createUserWithEmailAndPassword()" and Login with an already created Account. If it is successfull you can start the MainActivity.



mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}

// ...
}
});


Then in the onCreate callback of the MainActivity you can double check if the user is logged in with:



@Override
public void onCreate() {
super.onCreate();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null) {
updateUI(currentUser);
} else {
//intent back to login screen
}
}





share|improve this answer


























  • But how to make LoginActivity show up first if another activity is defined as main? Or, The main activity will still be first, however this double check procedure will either redirect to the LoginActivity(via intent), or leave the user in the mainactivity if he's logged? (So it's like a "circle": MainActivity > LoginActivity > MainActivity)?

    – TTnote
    Nov 21 '18 at 11:54








  • 1





    you can change the activity which will show up first in the AndroidManifest.xml. Just add a "launcher" and "main" intent filter under the activity you want to start with.

    – Tommy
    Nov 21 '18 at 12:29













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409829%2fwhat-is-the-correct-way-to-use-firebase-authentication%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You really should look up the Android Lifecycle. It is important to know when the callbacks(onCreate, onStart, onResume, etc...) are called and then you can decide on your own where to implement the code.



In your case I would make a LoginActivity where you also can register a new profile with "createUserWithEmailAndPassword()" and Login with an already created Account. If it is successfull you can start the MainActivity.



mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}

// ...
}
});


Then in the onCreate callback of the MainActivity you can double check if the user is logged in with:



@Override
public void onCreate() {
super.onCreate();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null) {
updateUI(currentUser);
} else {
//intent back to login screen
}
}





share|improve this answer


























  • But how to make LoginActivity show up first if another activity is defined as main? Or, The main activity will still be first, however this double check procedure will either redirect to the LoginActivity(via intent), or leave the user in the mainactivity if he's logged? (So it's like a "circle": MainActivity > LoginActivity > MainActivity)?

    – TTnote
    Nov 21 '18 at 11:54








  • 1





    you can change the activity which will show up first in the AndroidManifest.xml. Just add a "launcher" and "main" intent filter under the activity you want to start with.

    – Tommy
    Nov 21 '18 at 12:29


















3














You really should look up the Android Lifecycle. It is important to know when the callbacks(onCreate, onStart, onResume, etc...) are called and then you can decide on your own where to implement the code.



In your case I would make a LoginActivity where you also can register a new profile with "createUserWithEmailAndPassword()" and Login with an already created Account. If it is successfull you can start the MainActivity.



mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}

// ...
}
});


Then in the onCreate callback of the MainActivity you can double check if the user is logged in with:



@Override
public void onCreate() {
super.onCreate();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null) {
updateUI(currentUser);
} else {
//intent back to login screen
}
}





share|improve this answer


























  • But how to make LoginActivity show up first if another activity is defined as main? Or, The main activity will still be first, however this double check procedure will either redirect to the LoginActivity(via intent), or leave the user in the mainactivity if he's logged? (So it's like a "circle": MainActivity > LoginActivity > MainActivity)?

    – TTnote
    Nov 21 '18 at 11:54








  • 1





    you can change the activity which will show up first in the AndroidManifest.xml. Just add a "launcher" and "main" intent filter under the activity you want to start with.

    – Tommy
    Nov 21 '18 at 12:29
















3












3








3







You really should look up the Android Lifecycle. It is important to know when the callbacks(onCreate, onStart, onResume, etc...) are called and then you can decide on your own where to implement the code.



In your case I would make a LoginActivity where you also can register a new profile with "createUserWithEmailAndPassword()" and Login with an already created Account. If it is successfull you can start the MainActivity.



mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}

// ...
}
});


Then in the onCreate callback of the MainActivity you can double check if the user is logged in with:



@Override
public void onCreate() {
super.onCreate();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null) {
updateUI(currentUser);
} else {
//intent back to login screen
}
}





share|improve this answer















You really should look up the Android Lifecycle. It is important to know when the callbacks(onCreate, onStart, onResume, etc...) are called and then you can decide on your own where to implement the code.



In your case I would make a LoginActivity where you also can register a new profile with "createUserWithEmailAndPassword()" and Login with an already created Account. If it is successfull you can start the MainActivity.



mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}

// ...
}
});


Then in the onCreate callback of the MainActivity you can double check if the user is logged in with:



@Override
public void onCreate() {
super.onCreate();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null) {
updateUI(currentUser);
} else {
//intent back to login screen
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 19:17

























answered Nov 21 '18 at 11:02









TommyTommy

462




462













  • But how to make LoginActivity show up first if another activity is defined as main? Or, The main activity will still be first, however this double check procedure will either redirect to the LoginActivity(via intent), or leave the user in the mainactivity if he's logged? (So it's like a "circle": MainActivity > LoginActivity > MainActivity)?

    – TTnote
    Nov 21 '18 at 11:54








  • 1





    you can change the activity which will show up first in the AndroidManifest.xml. Just add a "launcher" and "main" intent filter under the activity you want to start with.

    – Tommy
    Nov 21 '18 at 12:29





















  • But how to make LoginActivity show up first if another activity is defined as main? Or, The main activity will still be first, however this double check procedure will either redirect to the LoginActivity(via intent), or leave the user in the mainactivity if he's logged? (So it's like a "circle": MainActivity > LoginActivity > MainActivity)?

    – TTnote
    Nov 21 '18 at 11:54








  • 1





    you can change the activity which will show up first in the AndroidManifest.xml. Just add a "launcher" and "main" intent filter under the activity you want to start with.

    – Tommy
    Nov 21 '18 at 12:29



















But how to make LoginActivity show up first if another activity is defined as main? Or, The main activity will still be first, however this double check procedure will either redirect to the LoginActivity(via intent), or leave the user in the mainactivity if he's logged? (So it's like a "circle": MainActivity > LoginActivity > MainActivity)?

– TTnote
Nov 21 '18 at 11:54







But how to make LoginActivity show up first if another activity is defined as main? Or, The main activity will still be first, however this double check procedure will either redirect to the LoginActivity(via intent), or leave the user in the mainactivity if he's logged? (So it's like a "circle": MainActivity > LoginActivity > MainActivity)?

– TTnote
Nov 21 '18 at 11:54






1




1





you can change the activity which will show up first in the AndroidManifest.xml. Just add a "launcher" and "main" intent filter under the activity you want to start with.

– Tommy
Nov 21 '18 at 12:29







you can change the activity which will show up first in the AndroidManifest.xml. Just add a "launcher" and "main" intent filter under the activity you want to start with.

– Tommy
Nov 21 '18 at 12:29




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409829%2fwhat-is-the-correct-way-to-use-firebase-authentication%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin