Android Firebase Facebook Login shows Logout button when app is reopened -


public class mainactivity extends appcompatactivity {      private loginbutton facebookloginbutton;     private callbackmanager callbackmanager;     private firebaseauth mauth;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         mauth = firebaseauth.getinstance();          facebookloginbutton=(loginbutton)findviewbyid(r.id.facebook_login_button);         callbackmanager=callbackmanager.factory.create();         facebookloginbutton.setreadpermissions("email", "public_profile");         facebookloginbutton.registercallback(callbackmanager, new facebookcallback<loginresult>() {             @override             public void onsuccess(loginresult loginresult) {                 handlefacebookaccesstoken(loginresult.getaccesstoken());             }              @override             public void oncancel() {              }              @override             public void onerror(facebookexception error) {                 toast.maketext(getapplicationcontext(), "error",toast.length_long).show();             }         });      }      @override     protected void onactivityresult(int requestcode, int resultcode, intent data) {         super.onactivityresult(requestcode, resultcode, data);         callbackmanager.onactivityresult(requestcode, resultcode, data);     }      private void handlefacebookaccesstoken(accesstoken token) {           authcredential credential = facebookauthprovider.getcredential(token.gettoken());         mauth.signinwithcredential(credential)                 .addoncompletelistener(this, new oncompletelistener<authresult>() {                     @override                     public void oncomplete(@nonnull task<authresult> task) {                         if (task.issuccessful()) {                             // sign in success, update ui signed-in user's information                              firebaseuser user = mauth.getcurrentuser();                             intent intent=new intent(getapplicationcontext(),nextactivity.class);                             //  intent.putextra("name",user.getdisplayname());                             startactivity(intent);                             finish();                          } else {                             // if sign in fails, display message user.                              toast.maketext(getapplicationcontext(), "authentication failed.",                                     toast.length_short).show();                          }                          // ...                     }                 });     }      @override     public void onstart() {         super.onstart();         // check if user signed in (non-null) , update ui accordingly.         firebaseuser currentuser = mauth.getcurrentuser(); //        currentuser.getdisplayname();     }  } 

i using firebase's facebook login. able login , if login success redirecting nextactivity.

but when pressed facebook login button, shows me logout button few seconds , redirects me nextactivity. when close app , open again, shows me mainactivity logout button.

i wanted display nextactivity if app closed , reopened again

you can save auth token after successful login facebook in application's shared preferences settings , whenever start app can check if there token saved in storage if means users have logged in , redirect directly him next page or profile page.

here small snippet can do:

final static string prefs_name = "auth"   public class mainactivity extends appcompatactivity{      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         settings = getsharedpreferences(prefs_name, mode_private);         setcontentview(r.layout.activity_main);          // check if user logged in         // i.e. auth token present or not         string token = settings.getstring("auth_token", null);         // means user logged in token found         if (token != null) {             auth_token = token;             startactivity(new intent(mainactivity.this, profileactivity.class));         }     }          // rest of code ....  });   @override public void oncomplete(@nonnull task<authresult> task) {     if (task.issuccessful()) {         // sign in success, update ui signed-in user's information          firebaseuser user = mauth.getcurrentuser();          // save user details or part of in shared prefs         sharedpreferences.editor editor = settings.edit();         editor.putstring("user", user);         editor.apply();          intent intent=new intent(getapplicationcontext(),nextactivity.class);         //  intent.putextra("name",user.getdisplayname());         startactivity(intent);         finish();      } else {         // if sign in fails, display message user.          toast.maketext(getapplicationcontext(), "authentication failed.",                 toast.length_short).show();      }      // ... } 

this work since every time launch app in oncreate method checks auth file if it's present directly send user nextactivity.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -