android - Facebook SDK(4.x): AccessToken.getCurrentAccessToken() returns null during run and not null in debug mode -
method one:in case set place holder objects current accesstoken , profile
facebooksdk.sdkinitialize(getapplicationcontext()); // initialize sdk before executing other operations, // especially, if you're using facebook ui elements. accesstoken accesstoken = accesstoken.getcurrentaccesstoken(); profile profile = profile.getcurrentprofile(); //attempts recognize if user has logged in before if(accesstoken !=null){ if(profile !=null) { //set user parceable //start intent next activity //in final code intent 3 tabbed activity (faves, nearby, specials) //for navigating nearby intent intent = new intent(splash.this, nearbyplacesholder.class); startactivity(intent); finish(); } else{ } }
method two: use if statement check if getcurrentaccesstoken() == null , if set boolean islogged in false. if getcurrentaccesstoken() == null condition returns set isloggedin true , perform if statement on isloggedin.
the problem inside of debug mode code works perfectly, when run it, no go, app stays login activity , requires user perform login. doesn't prompt actual login overlays spinning circle , continues. profile , accesstoken there.
instead of trying retrieve profile again, after login facebook save info want, including fb id, sqlite db. no issues anymore.
old:
as of right code catching isloggedin 95% of time. not 100% tho. think timing issue, wish there way make sure isloggedin() doesn't called until facbook resources ready.
@override protected void oncreate(bundle savedinstancestate) { //remove title bar this.requestwindowfeature(window.feature_no_title); //set view saved data , change layout super.oncreate(savedinstancestate); setcontentview(r.layout.activity_splash); facebooksdk.sdkinitialize(getapplicationcontext()); // initialize sdk before executing other operations, // especially, if you're using facebook ui elements. /*~~~~~~ google api setup */ mgoogleapiclient = new googleapiclient.builder(splash.this) .addconnectioncallbacks(splash.this) .addonconnectionfailedlistener(splash.this) .addapi(plus.api) .addscope(plus.scope_plus_profile) .build(); /*~~~~~~~~~~~~~~ facebook sdk setup , login event handler */ callbackmanager = callbackmanager.factory.create(); //needs set in on activity result loginmanager.getinstance().registercallback(callbackmanager, new facebookcallback<loginresult>() { @override public void onsuccess(loginresult loginresult) { intent intent = new intent(splash.this, nearbyplacesholder.class); startactivity(intent); } @override public void oncancel() { // app code log.d("fblogin", "cancel"); toast.maketext(splash.this, "facebook login canceled", toast.length_short).show(); } @override public void onerror(facebookexception exception) { // app code log.d("fblogin", "error"); } }); //initializes facebook seekbar event handlers //may adding noises in here @ later date final seekbar fb_seekbar = (seekbar)findviewbyid(r.id.seekbar_fb); fb_seekbar.setonseekbarchangelistener(new seekbar.onseekbarchangelistener() { @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { fb_progress = progress; } @override public void onstarttrackingtouch(seekbar seekbar) { } @override public void onstoptrackingtouch(seekbar seekbar) { if (fb_progress <= 75) { fb_seekbar.setprogress(4); } else { fb_seekbar.setprogress(100); //this facebook sign in performed loginmanager.getinstance().loginwithreadpermissions(splash.this, arrays.aslist("public_profile", "user_friends")); //start intent next activity //in final code intent 3 tabbed activity (faves, nearby, specials) //for navigating nearby //intent intent = new intent(splash.this, nearbyplacesholder.class); //startactivity(intent); } } }); //initializes google plus seekbar event handlers //may adding noises in here @ later date final seekbar google_slider = (seekbar)findviewbyid(r.id.seekbar_google); google_slider.setonseekbarchangelistener(new seekbar.onseekbarchangelistener() { @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { google_progress = progress; } @override public void onstarttrackingtouch(seekbar seekbar) { } @override public void onstoptrackingtouch(seekbar seekbar) { if (google_progress <= 75) { google_slider.setprogress(0); } else { google_slider.setprogress(100); mgoogleapiclient.connect(); } } }); //initializes sign seekbar event handlers //may adding noises in here @ later date final seekbar sign_up_seekbar = (seekbar)findviewbyid(r.id.seekbar_regular); sign_up_seekbar.setonseekbarchangelistener(new seekbar.onseekbarchangelistener() { @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { sign_up_progress = progress; } @override public void onstarttrackingtouch(seekbar seekbar) { } @override public void onstoptrackingtouch(seekbar seekbar) { if (sign_up_progress <= 75) { sign_up_seekbar.setprogress(4); } else { sign_up_seekbar.setprogress(100); //this facebook sign in performed //start intent next activity //in final code intent 3 tabbed activity (faves, nearby, specials) //for navigating nearby //intent intent = new intent(splash.this, makeprofile.class); //startactivity(intent); } } }); if(isloggedin()){ //start intent next activity //in final code intent 3 tabbed activity (faves, nearby, specials) //for navigating nearby intent intent = new intent(splash.this, nearbyplacesholder.class); startactivity(intent); finish(); } } public boolean isloggedin(){ accesstoken accesstoken = accesstoken.getcurrentaccesstoken(); profile profile = profile.getcurrentprofile(); //attempts recognize if user has logged in before if(accesstoken !=null && profile !=null) { return true; } else{ return false; } }
Comments
Post a Comment