One time login in app - FirebaseAuth












4















I'm working on an app that uses Firebase authentication to sign in users through phone number. I want to add a functionality such that there is only one-time login for the user, i.e. even if the user kills the app and starts it again, he should be logged in. Also, I don't want to add a logout feature. What can be done for this?










share|improve this question





























    4















    I'm working on an app that uses Firebase authentication to sign in users through phone number. I want to add a functionality such that there is only one-time login for the user, i.e. even if the user kills the app and starts it again, he should be logged in. Also, I don't want to add a logout feature. What can be done for this?










    share|improve this question



























      4












      4








      4


      2






      I'm working on an app that uses Firebase authentication to sign in users through phone number. I want to add a functionality such that there is only one-time login for the user, i.e. even if the user kills the app and starts it again, he should be logged in. Also, I don't want to add a logout feature. What can be done for this?










      share|improve this question
















      I'm working on an app that uses Firebase authentication to sign in users through phone number. I want to add a functionality such that there is only one-time login for the user, i.e. even if the user kills the app and starts it again, he should be logged in. Also, I don't want to add a logout feature. What can be done for this?







      java android firebase firebase-authentication






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 16 '18 at 10:05









      Alex Mamo

      46.1k82965




      46.1k82965










      asked Jun 16 '18 at 7:28









      Einzig7Einzig7

      490418




      490418
























          3 Answers
          3






          active

          oldest

          votes


















          6














          The simplest way to achieve this is to use a listener. Let's assume you have two activities, the LoginActivity and the MainActivity. The listener that can be created in the LoginActivity should look like this:



          FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
          @Override
          public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
          FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
          if (firebaseUser != null) {
          Intent intent = new Intent(LoginActivity.this, MainActivity.class);
          startActivity(intent);
          finish();
          }
          }
          };


          This basically means that if the user is logged in, skip the LoginActivity and go to the MainActivity.



          Instantiate the FirebaseAuth object:



          FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();


          And start listening for changes in your onStart() method like this:



          @Override
          protected void onStart() {
          super.onStart();
          firebaseAuth.addAuthStateListener(authStateListener);
          }


          In the MainActivity, you should do the same thing:



          FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
          @Override
          public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
          FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
          if (firebaseUser == null) {
          Intent intent = new Intent(MainActivity.this, LoginActivity.class);
          startActivity(intent);
          }
          }
          };


          Which basically means that if the user is not logged in, skip the MainActivity and go to the LoginActivity. In this activity you should do the same thing as in the LoginActivity, you should start listening for changes in the onStart().



          In both activities, don't forget to remove the listener in the moment in which is not needed anymore. So add the following line of code in your onStop() method:



          @Override
          protected void onStop() {
          super.onStop();
          firebaseAuth.removeAuthStateListener(authStateListener);
          }





          share|improve this answer


























          • Have you also tried my solution above?

            – Alex Mamo
            Jun 18 '18 at 5:22











          • Yes, it worked! But I think shared preferences too would be good to use.

            – Einzig7
            Jun 19 '18 at 12:32











          • It would be if don't care about app uninstall. SharedPreferences do not persist across app uninstall while Firebase keeps authentication data in a back-up even if the application is deleted. So in this case, if you uninstall the app, there will be no loss of informations. It's up to you to decide which solution is better for you.

            – Alex Mamo
            Jun 19 '18 at 12:43













          • Thanks a lot, Alex! Actually I'm making an app for visually impaired people, and using now I realize that using authStateListener will be the right decision.

            – Einzig7
            Jun 19 '18 at 12:54











          • Glad I could help @Einzig7 cheers!

            – Alex Mamo
            Jun 19 '18 at 12:57



















          2














          You can save user login session in shared Preference



          Do this when your login got Login Success



          SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", Activity.MODE_PRIVATE);
          Editor editor = pref.edit();
          editor.putBoolean("key_name", true); // Storing boolean - true/false
          editor.commit();


          and When Your apps is Starts (like Splash or login Page) use this



           SharedPreferences sp=this.getSharedPreferences("MyPref", Context.MODE_PRIVATE);
          boolean b = sp.getBoolean("key_name", false);
          if(b==true){
          //User Already Logged in skip login page and continue to next process
          }else{
          //User is Not logged in, show login Form
          }


          It will works for you.






          share|improve this answer


























          • what is the strFile parameter here?

            – Einzig7
            Jun 16 '18 at 8:25













          • It is the preference Name, i forget to change it ,now i updated answer @Einzig7

            – Sandeep Parish
            Jun 16 '18 at 8:28













          • Also, I'm getting an error here for the getBoolean function, it says that getBoolean has two parameters, a string and a boolean.

            – Einzig7
            Jun 16 '18 at 8:33











          • It wants a default value set it false

            – Sandeep Parish
            Jun 16 '18 at 8:40











          • It worked! Thanks :)

            – Einzig7
            Jun 16 '18 at 9:05



















          0














          This is from my working code from logging class:



             private void LoginUserAccount(String email, String password)
          {
          if (TextUtils.isEmpty(email))
          {
          Toast.makeText(ResponseStartPage.this, "Please write Your Email", Toast.LENGTH_SHORT).show();
          }
          if (TextUtils.isEmpty(password))
          {
          Toast.makeText(ResponseStartPage.this, "Please write Your password", Toast.LENGTH_SHORT).show();
          }

          else
          {
          loadingBar.setTitle("Login Account");
          loadingBar.setMessage("Please wait for verification...");
          loadingBar.show();
          mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
          @Override
          public void onComplete(@NonNull Task<AuthResult> task)
          {
          if (task.isSuccessful())
          {
          String online_user_id = mAuth.getCurrentUser().getUid();
          String DeviceToken = FirebaseInstanceId.getInstance().getToken();

          usersReference.child(online_user_id).child("device_token").setValue(DeviceToken)
          .addOnSuccessListener(new OnSuccessListener<Void>() {
          @Override
          public void onSuccess(Void aVoid)
          {
          Intent mainIntent = new Intent(ResponseStartPage.this, ResponseMainActivity.class);
          mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
          startActivity(mainIntent);
          finish();
          }
          });


          }
          else
          {
          Toast.makeText(ResponseStartPage.this, "Please Check your email and password", Toast.LENGTH_SHORT).show();
          }
          loadingBar.dismiss();
          }
          });
          }
          }





          share|improve this answer























            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%2f50885891%2fone-time-login-in-app-firebaseauth%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            6














            The simplest way to achieve this is to use a listener. Let's assume you have two activities, the LoginActivity and the MainActivity. The listener that can be created in the LoginActivity should look like this:



            FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
            if (firebaseUser != null) {
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
            }
            }
            };


            This basically means that if the user is logged in, skip the LoginActivity and go to the MainActivity.



            Instantiate the FirebaseAuth object:



            FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();


            And start listening for changes in your onStart() method like this:



            @Override
            protected void onStart() {
            super.onStart();
            firebaseAuth.addAuthStateListener(authStateListener);
            }


            In the MainActivity, you should do the same thing:



            FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
            if (firebaseUser == null) {
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            startActivity(intent);
            }
            }
            };


            Which basically means that if the user is not logged in, skip the MainActivity and go to the LoginActivity. In this activity you should do the same thing as in the LoginActivity, you should start listening for changes in the onStart().



            In both activities, don't forget to remove the listener in the moment in which is not needed anymore. So add the following line of code in your onStop() method:



            @Override
            protected void onStop() {
            super.onStop();
            firebaseAuth.removeAuthStateListener(authStateListener);
            }





            share|improve this answer


























            • Have you also tried my solution above?

              – Alex Mamo
              Jun 18 '18 at 5:22











            • Yes, it worked! But I think shared preferences too would be good to use.

              – Einzig7
              Jun 19 '18 at 12:32











            • It would be if don't care about app uninstall. SharedPreferences do not persist across app uninstall while Firebase keeps authentication data in a back-up even if the application is deleted. So in this case, if you uninstall the app, there will be no loss of informations. It's up to you to decide which solution is better for you.

              – Alex Mamo
              Jun 19 '18 at 12:43













            • Thanks a lot, Alex! Actually I'm making an app for visually impaired people, and using now I realize that using authStateListener will be the right decision.

              – Einzig7
              Jun 19 '18 at 12:54











            • Glad I could help @Einzig7 cheers!

              – Alex Mamo
              Jun 19 '18 at 12:57
















            6














            The simplest way to achieve this is to use a listener. Let's assume you have two activities, the LoginActivity and the MainActivity. The listener that can be created in the LoginActivity should look like this:



            FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
            if (firebaseUser != null) {
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
            }
            }
            };


            This basically means that if the user is logged in, skip the LoginActivity and go to the MainActivity.



            Instantiate the FirebaseAuth object:



            FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();


            And start listening for changes in your onStart() method like this:



            @Override
            protected void onStart() {
            super.onStart();
            firebaseAuth.addAuthStateListener(authStateListener);
            }


            In the MainActivity, you should do the same thing:



            FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
            if (firebaseUser == null) {
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            startActivity(intent);
            }
            }
            };


            Which basically means that if the user is not logged in, skip the MainActivity and go to the LoginActivity. In this activity you should do the same thing as in the LoginActivity, you should start listening for changes in the onStart().



            In both activities, don't forget to remove the listener in the moment in which is not needed anymore. So add the following line of code in your onStop() method:



            @Override
            protected void onStop() {
            super.onStop();
            firebaseAuth.removeAuthStateListener(authStateListener);
            }





            share|improve this answer


























            • Have you also tried my solution above?

              – Alex Mamo
              Jun 18 '18 at 5:22











            • Yes, it worked! But I think shared preferences too would be good to use.

              – Einzig7
              Jun 19 '18 at 12:32











            • It would be if don't care about app uninstall. SharedPreferences do not persist across app uninstall while Firebase keeps authentication data in a back-up even if the application is deleted. So in this case, if you uninstall the app, there will be no loss of informations. It's up to you to decide which solution is better for you.

              – Alex Mamo
              Jun 19 '18 at 12:43













            • Thanks a lot, Alex! Actually I'm making an app for visually impaired people, and using now I realize that using authStateListener will be the right decision.

              – Einzig7
              Jun 19 '18 at 12:54











            • Glad I could help @Einzig7 cheers!

              – Alex Mamo
              Jun 19 '18 at 12:57














            6












            6








            6







            The simplest way to achieve this is to use a listener. Let's assume you have two activities, the LoginActivity and the MainActivity. The listener that can be created in the LoginActivity should look like this:



            FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
            if (firebaseUser != null) {
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
            }
            }
            };


            This basically means that if the user is logged in, skip the LoginActivity and go to the MainActivity.



            Instantiate the FirebaseAuth object:



            FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();


            And start listening for changes in your onStart() method like this:



            @Override
            protected void onStart() {
            super.onStart();
            firebaseAuth.addAuthStateListener(authStateListener);
            }


            In the MainActivity, you should do the same thing:



            FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
            if (firebaseUser == null) {
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            startActivity(intent);
            }
            }
            };


            Which basically means that if the user is not logged in, skip the MainActivity and go to the LoginActivity. In this activity you should do the same thing as in the LoginActivity, you should start listening for changes in the onStart().



            In both activities, don't forget to remove the listener in the moment in which is not needed anymore. So add the following line of code in your onStop() method:



            @Override
            protected void onStop() {
            super.onStop();
            firebaseAuth.removeAuthStateListener(authStateListener);
            }





            share|improve this answer















            The simplest way to achieve this is to use a listener. Let's assume you have two activities, the LoginActivity and the MainActivity. The listener that can be created in the LoginActivity should look like this:



            FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
            if (firebaseUser != null) {
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
            }
            }
            };


            This basically means that if the user is logged in, skip the LoginActivity and go to the MainActivity.



            Instantiate the FirebaseAuth object:



            FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();


            And start listening for changes in your onStart() method like this:



            @Override
            protected void onStart() {
            super.onStart();
            firebaseAuth.addAuthStateListener(authStateListener);
            }


            In the MainActivity, you should do the same thing:



            FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
            if (firebaseUser == null) {
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            startActivity(intent);
            }
            }
            };


            Which basically means that if the user is not logged in, skip the MainActivity and go to the LoginActivity. In this activity you should do the same thing as in the LoginActivity, you should start listening for changes in the onStart().



            In both activities, don't forget to remove the listener in the moment in which is not needed anymore. So add the following line of code in your onStop() method:



            @Override
            protected void onStop() {
            super.onStop();
            firebaseAuth.removeAuthStateListener(authStateListener);
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 28 '18 at 14:50









            Frank van Puffelen

            242k29387414




            242k29387414










            answered Jun 16 '18 at 10:04









            Alex MamoAlex Mamo

            46.1k82965




            46.1k82965













            • Have you also tried my solution above?

              – Alex Mamo
              Jun 18 '18 at 5:22











            • Yes, it worked! But I think shared preferences too would be good to use.

              – Einzig7
              Jun 19 '18 at 12:32











            • It would be if don't care about app uninstall. SharedPreferences do not persist across app uninstall while Firebase keeps authentication data in a back-up even if the application is deleted. So in this case, if you uninstall the app, there will be no loss of informations. It's up to you to decide which solution is better for you.

              – Alex Mamo
              Jun 19 '18 at 12:43













            • Thanks a lot, Alex! Actually I'm making an app for visually impaired people, and using now I realize that using authStateListener will be the right decision.

              – Einzig7
              Jun 19 '18 at 12:54











            • Glad I could help @Einzig7 cheers!

              – Alex Mamo
              Jun 19 '18 at 12:57



















            • Have you also tried my solution above?

              – Alex Mamo
              Jun 18 '18 at 5:22











            • Yes, it worked! But I think shared preferences too would be good to use.

              – Einzig7
              Jun 19 '18 at 12:32











            • It would be if don't care about app uninstall. SharedPreferences do not persist across app uninstall while Firebase keeps authentication data in a back-up even if the application is deleted. So in this case, if you uninstall the app, there will be no loss of informations. It's up to you to decide which solution is better for you.

              – Alex Mamo
              Jun 19 '18 at 12:43













            • Thanks a lot, Alex! Actually I'm making an app for visually impaired people, and using now I realize that using authStateListener will be the right decision.

              – Einzig7
              Jun 19 '18 at 12:54











            • Glad I could help @Einzig7 cheers!

              – Alex Mamo
              Jun 19 '18 at 12:57

















            Have you also tried my solution above?

            – Alex Mamo
            Jun 18 '18 at 5:22





            Have you also tried my solution above?

            – Alex Mamo
            Jun 18 '18 at 5:22













            Yes, it worked! But I think shared preferences too would be good to use.

            – Einzig7
            Jun 19 '18 at 12:32





            Yes, it worked! But I think shared preferences too would be good to use.

            – Einzig7
            Jun 19 '18 at 12:32













            It would be if don't care about app uninstall. SharedPreferences do not persist across app uninstall while Firebase keeps authentication data in a back-up even if the application is deleted. So in this case, if you uninstall the app, there will be no loss of informations. It's up to you to decide which solution is better for you.

            – Alex Mamo
            Jun 19 '18 at 12:43







            It would be if don't care about app uninstall. SharedPreferences do not persist across app uninstall while Firebase keeps authentication data in a back-up even if the application is deleted. So in this case, if you uninstall the app, there will be no loss of informations. It's up to you to decide which solution is better for you.

            – Alex Mamo
            Jun 19 '18 at 12:43















            Thanks a lot, Alex! Actually I'm making an app for visually impaired people, and using now I realize that using authStateListener will be the right decision.

            – Einzig7
            Jun 19 '18 at 12:54





            Thanks a lot, Alex! Actually I'm making an app for visually impaired people, and using now I realize that using authStateListener will be the right decision.

            – Einzig7
            Jun 19 '18 at 12:54













            Glad I could help @Einzig7 cheers!

            – Alex Mamo
            Jun 19 '18 at 12:57





            Glad I could help @Einzig7 cheers!

            – Alex Mamo
            Jun 19 '18 at 12:57













            2














            You can save user login session in shared Preference



            Do this when your login got Login Success



            SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", Activity.MODE_PRIVATE);
            Editor editor = pref.edit();
            editor.putBoolean("key_name", true); // Storing boolean - true/false
            editor.commit();


            and When Your apps is Starts (like Splash or login Page) use this



             SharedPreferences sp=this.getSharedPreferences("MyPref", Context.MODE_PRIVATE);
            boolean b = sp.getBoolean("key_name", false);
            if(b==true){
            //User Already Logged in skip login page and continue to next process
            }else{
            //User is Not logged in, show login Form
            }


            It will works for you.






            share|improve this answer


























            • what is the strFile parameter here?

              – Einzig7
              Jun 16 '18 at 8:25













            • It is the preference Name, i forget to change it ,now i updated answer @Einzig7

              – Sandeep Parish
              Jun 16 '18 at 8:28













            • Also, I'm getting an error here for the getBoolean function, it says that getBoolean has two parameters, a string and a boolean.

              – Einzig7
              Jun 16 '18 at 8:33











            • It wants a default value set it false

              – Sandeep Parish
              Jun 16 '18 at 8:40











            • It worked! Thanks :)

              – Einzig7
              Jun 16 '18 at 9:05
















            2














            You can save user login session in shared Preference



            Do this when your login got Login Success



            SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", Activity.MODE_PRIVATE);
            Editor editor = pref.edit();
            editor.putBoolean("key_name", true); // Storing boolean - true/false
            editor.commit();


            and When Your apps is Starts (like Splash or login Page) use this



             SharedPreferences sp=this.getSharedPreferences("MyPref", Context.MODE_PRIVATE);
            boolean b = sp.getBoolean("key_name", false);
            if(b==true){
            //User Already Logged in skip login page and continue to next process
            }else{
            //User is Not logged in, show login Form
            }


            It will works for you.






            share|improve this answer


























            • what is the strFile parameter here?

              – Einzig7
              Jun 16 '18 at 8:25













            • It is the preference Name, i forget to change it ,now i updated answer @Einzig7

              – Sandeep Parish
              Jun 16 '18 at 8:28













            • Also, I'm getting an error here for the getBoolean function, it says that getBoolean has two parameters, a string and a boolean.

              – Einzig7
              Jun 16 '18 at 8:33











            • It wants a default value set it false

              – Sandeep Parish
              Jun 16 '18 at 8:40











            • It worked! Thanks :)

              – Einzig7
              Jun 16 '18 at 9:05














            2












            2








            2







            You can save user login session in shared Preference



            Do this when your login got Login Success



            SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", Activity.MODE_PRIVATE);
            Editor editor = pref.edit();
            editor.putBoolean("key_name", true); // Storing boolean - true/false
            editor.commit();


            and When Your apps is Starts (like Splash or login Page) use this



             SharedPreferences sp=this.getSharedPreferences("MyPref", Context.MODE_PRIVATE);
            boolean b = sp.getBoolean("key_name", false);
            if(b==true){
            //User Already Logged in skip login page and continue to next process
            }else{
            //User is Not logged in, show login Form
            }


            It will works for you.






            share|improve this answer















            You can save user login session in shared Preference



            Do this when your login got Login Success



            SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", Activity.MODE_PRIVATE);
            Editor editor = pref.edit();
            editor.putBoolean("key_name", true); // Storing boolean - true/false
            editor.commit();


            and When Your apps is Starts (like Splash or login Page) use this



             SharedPreferences sp=this.getSharedPreferences("MyPref", Context.MODE_PRIVATE);
            boolean b = sp.getBoolean("key_name", false);
            if(b==true){
            //User Already Logged in skip login page and continue to next process
            }else{
            //User is Not logged in, show login Form
            }


            It will works for you.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 6 '18 at 5:50

























            answered Jun 16 '18 at 8:10









            Sandeep ParishSandeep Parish

            1,1871315




            1,1871315













            • what is the strFile parameter here?

              – Einzig7
              Jun 16 '18 at 8:25













            • It is the preference Name, i forget to change it ,now i updated answer @Einzig7

              – Sandeep Parish
              Jun 16 '18 at 8:28













            • Also, I'm getting an error here for the getBoolean function, it says that getBoolean has two parameters, a string and a boolean.

              – Einzig7
              Jun 16 '18 at 8:33











            • It wants a default value set it false

              – Sandeep Parish
              Jun 16 '18 at 8:40











            • It worked! Thanks :)

              – Einzig7
              Jun 16 '18 at 9:05



















            • what is the strFile parameter here?

              – Einzig7
              Jun 16 '18 at 8:25













            • It is the preference Name, i forget to change it ,now i updated answer @Einzig7

              – Sandeep Parish
              Jun 16 '18 at 8:28













            • Also, I'm getting an error here for the getBoolean function, it says that getBoolean has two parameters, a string and a boolean.

              – Einzig7
              Jun 16 '18 at 8:33











            • It wants a default value set it false

              – Sandeep Parish
              Jun 16 '18 at 8:40











            • It worked! Thanks :)

              – Einzig7
              Jun 16 '18 at 9:05

















            what is the strFile parameter here?

            – Einzig7
            Jun 16 '18 at 8:25







            what is the strFile parameter here?

            – Einzig7
            Jun 16 '18 at 8:25















            It is the preference Name, i forget to change it ,now i updated answer @Einzig7

            – Sandeep Parish
            Jun 16 '18 at 8:28







            It is the preference Name, i forget to change it ,now i updated answer @Einzig7

            – Sandeep Parish
            Jun 16 '18 at 8:28















            Also, I'm getting an error here for the getBoolean function, it says that getBoolean has two parameters, a string and a boolean.

            – Einzig7
            Jun 16 '18 at 8:33





            Also, I'm getting an error here for the getBoolean function, it says that getBoolean has two parameters, a string and a boolean.

            – Einzig7
            Jun 16 '18 at 8:33













            It wants a default value set it false

            – Sandeep Parish
            Jun 16 '18 at 8:40





            It wants a default value set it false

            – Sandeep Parish
            Jun 16 '18 at 8:40













            It worked! Thanks :)

            – Einzig7
            Jun 16 '18 at 9:05





            It worked! Thanks :)

            – Einzig7
            Jun 16 '18 at 9:05











            0














            This is from my working code from logging class:



               private void LoginUserAccount(String email, String password)
            {
            if (TextUtils.isEmpty(email))
            {
            Toast.makeText(ResponseStartPage.this, "Please write Your Email", Toast.LENGTH_SHORT).show();
            }
            if (TextUtils.isEmpty(password))
            {
            Toast.makeText(ResponseStartPage.this, "Please write Your password", Toast.LENGTH_SHORT).show();
            }

            else
            {
            loadingBar.setTitle("Login Account");
            loadingBar.setMessage("Please wait for verification...");
            loadingBar.show();
            mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task)
            {
            if (task.isSuccessful())
            {
            String online_user_id = mAuth.getCurrentUser().getUid();
            String DeviceToken = FirebaseInstanceId.getInstance().getToken();

            usersReference.child(online_user_id).child("device_token").setValue(DeviceToken)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid)
            {
            Intent mainIntent = new Intent(ResponseStartPage.this, ResponseMainActivity.class);
            mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(mainIntent);
            finish();
            }
            });


            }
            else
            {
            Toast.makeText(ResponseStartPage.this, "Please Check your email and password", Toast.LENGTH_SHORT).show();
            }
            loadingBar.dismiss();
            }
            });
            }
            }





            share|improve this answer




























              0














              This is from my working code from logging class:



                 private void LoginUserAccount(String email, String password)
              {
              if (TextUtils.isEmpty(email))
              {
              Toast.makeText(ResponseStartPage.this, "Please write Your Email", Toast.LENGTH_SHORT).show();
              }
              if (TextUtils.isEmpty(password))
              {
              Toast.makeText(ResponseStartPage.this, "Please write Your password", Toast.LENGTH_SHORT).show();
              }

              else
              {
              loadingBar.setTitle("Login Account");
              loadingBar.setMessage("Please wait for verification...");
              loadingBar.show();
              mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
              @Override
              public void onComplete(@NonNull Task<AuthResult> task)
              {
              if (task.isSuccessful())
              {
              String online_user_id = mAuth.getCurrentUser().getUid();
              String DeviceToken = FirebaseInstanceId.getInstance().getToken();

              usersReference.child(online_user_id).child("device_token").setValue(DeviceToken)
              .addOnSuccessListener(new OnSuccessListener<Void>() {
              @Override
              public void onSuccess(Void aVoid)
              {
              Intent mainIntent = new Intent(ResponseStartPage.this, ResponseMainActivity.class);
              mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
              startActivity(mainIntent);
              finish();
              }
              });


              }
              else
              {
              Toast.makeText(ResponseStartPage.this, "Please Check your email and password", Toast.LENGTH_SHORT).show();
              }
              loadingBar.dismiss();
              }
              });
              }
              }





              share|improve this answer


























                0












                0








                0







                This is from my working code from logging class:



                   private void LoginUserAccount(String email, String password)
                {
                if (TextUtils.isEmpty(email))
                {
                Toast.makeText(ResponseStartPage.this, "Please write Your Email", Toast.LENGTH_SHORT).show();
                }
                if (TextUtils.isEmpty(password))
                {
                Toast.makeText(ResponseStartPage.this, "Please write Your password", Toast.LENGTH_SHORT).show();
                }

                else
                {
                loadingBar.setTitle("Login Account");
                loadingBar.setMessage("Please wait for verification...");
                loadingBar.show();
                mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task)
                {
                if (task.isSuccessful())
                {
                String online_user_id = mAuth.getCurrentUser().getUid();
                String DeviceToken = FirebaseInstanceId.getInstance().getToken();

                usersReference.child(online_user_id).child("device_token").setValue(DeviceToken)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid)
                {
                Intent mainIntent = new Intent(ResponseStartPage.this, ResponseMainActivity.class);
                mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(mainIntent);
                finish();
                }
                });


                }
                else
                {
                Toast.makeText(ResponseStartPage.this, "Please Check your email and password", Toast.LENGTH_SHORT).show();
                }
                loadingBar.dismiss();
                }
                });
                }
                }





                share|improve this answer













                This is from my working code from logging class:



                   private void LoginUserAccount(String email, String password)
                {
                if (TextUtils.isEmpty(email))
                {
                Toast.makeText(ResponseStartPage.this, "Please write Your Email", Toast.LENGTH_SHORT).show();
                }
                if (TextUtils.isEmpty(password))
                {
                Toast.makeText(ResponseStartPage.this, "Please write Your password", Toast.LENGTH_SHORT).show();
                }

                else
                {
                loadingBar.setTitle("Login Account");
                loadingBar.setMessage("Please wait for verification...");
                loadingBar.show();
                mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task)
                {
                if (task.isSuccessful())
                {
                String online_user_id = mAuth.getCurrentUser().getUid();
                String DeviceToken = FirebaseInstanceId.getInstance().getToken();

                usersReference.child(online_user_id).child("device_token").setValue(DeviceToken)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid)
                {
                Intent mainIntent = new Intent(ResponseStartPage.this, ResponseMainActivity.class);
                mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(mainIntent);
                finish();
                }
                });


                }
                else
                {
                Toast.makeText(ResponseStartPage.this, "Please Check your email and password", Toast.LENGTH_SHORT).show();
                }
                loadingBar.dismiss();
                }
                });
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 16 '18 at 8:17









                JacekJacek

                237




                237






























                    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%2f50885891%2fone-time-login-in-app-firebaseauth%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

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks