button - How do I make an activity wait until Facebook login is complete? -
i trying make application android platform. far have 2 activities. both have fragments. first activity @ moment using facebook api users can sign in. want first activity switch second activity once login complete. tried using onclicklistener intent, second activity start before login completed. have sense reverted code point there no onclicklistener or intent. appreciated. did try reading documentation intents , activities, couldn't figure out how wanted. post code bellow.
public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); }
}
next
package com.tfs.taylor.retreat; import android.content.intent; import android.support.v4.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.textview; import com.facebook.accesstoken; import com.facebook.accesstokentracker; import com.facebook.callbackmanager; import com.facebook.facebookcallback; import com.facebook.facebookexception; import com.facebook.facebooksdk; import com.facebook.profile; import com.facebook.profiletracker; import com.facebook.login.loginresult; import com.facebook.login.widget.loginbutton; /** * placeholder fragment containing simple view. */ public class mainfragment extends fragment { private textview mtextdetails; private accesstokentracker mtokentracker; private profiletracker mprofiletracker; private callbackmanager mcallbackmanager; private facebookcallback<loginresult> mcallback= new facebookcallback<loginresult>() { @override public void onsuccess(loginresult loginresult) { accesstoken accesstoken = loginresult.getaccesstoken(); profile profile = profile.getcurrentprofile(); displaywelcomemessage(profile); } @override public void oncancel() { } @override public void onerror(facebookexception e) { } }; public mainfragment() { } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); facebooksdk.sdkinitialize(getactivity().getapplicationcontext()); mcallbackmanager=callbackmanager.factory.create(); mtokentracker=new accesstokentracker() { @override protected void oncurrentaccesstokenchanged(accesstoken oldtoken, accesstoken newtoken) { } }; mprofiletracker=new profiletracker() { @override protected void oncurrentprofilechanged(profile oldprofile, profile newprofile) { } }; mtokentracker.starttracking(); mprofiletracker.starttracking(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.fragment_main, container, false); } private void displaywelcomemessage(profile profile) { if (profile != null) { mtextdetails.settext("welcome " + profile.getname()); } } @override public void onviewcreated(view view, bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); loginbutton loginbutton = (loginbutton) view.findviewbyid(r.id.login_button); loginbutton.setreadpermissions("public_profile"); loginbutton.setfragment(this); loginbutton.registercallback(mcallbackmanager, mcallback); mtextdetails=(textview) view.findviewbyid(r.id.text_details); } @override public void onresume() { super.onresume(); profile profile = profile.getcurrentprofile(); displaywelcomemessage(profile); } @override public void onstop() { super.onstop(); mtokentracker.stoptracking(); mprofiletracker.stoptracking(); } @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); mcallbackmanager.onactivityresult(requestcode,resultcode,data); }
}
you have call new activity in onsuccess method. activity called after user logged in successfully.
private facebookcallback<loginresult> mcallback= new facebookcallback<loginresult>() { @override public void onsuccess(loginresult loginresult) { accesstoken accesstoken = loginresult.getaccesstoken(); profile profile = profile.getcurrentprofile(); displaywelcomemessage(profile); //call new activity here. }
Comments
Post a Comment