android - Facebook Session is already opened -
i have updated facebook sdk code used work no longer works.
i getting following exception:
06-10 10:37:58.146: e/androidruntime(28184): java.lang.unsupportedoperationexception: session: attempt made open opened session. 06-10 10:37:58.146: e/androidruntime(28184): @ com.facebook.session.open(session.java:985) 06-10 10:37:58.146: e/androidruntime(28184): @ com.facebook.session.openforread(session.java:388)
and have following code open session:
session session = session.getactivesession(); if (session == null) { session = new session(dealactivity.this); } //session.isopened() apparently false! if (!session.isopened()) { session.openrequest openrequest = null; openrequest = new session.openrequest(this); openrequest.setpermissions(facebookmanager.read_permissions); openrequest.setloginbehavior(sessionloginbehavior.sso_with_fallback); openrequest.setcallback(callback); session.openforread(openrequest); return; } else { getdatafromfacebook(); }
as using facebook uilifecyclehelper
object in needed callbacks(it used work).
and onactivityresult
callback have tried calling first this:
uihelper.onactivityresult(requestcode, resultcode, data);
and following code:
session session = session.getactivesession(); if (session != null) { session.onactivityresult(dealactivity.this, requestcode, resultcode, data); }
both of them didn't work.
this facebook session.statuscallback
private session.statuscallback callback = new session.statuscallback() { @override public void call(session session, sessionstate state, exception exception) { onsessionstatechange(session, state, exception); } }; private void onsessionstatechange(session session, sessionstate state, exception exception) { if (exception != null) { exception.printstacktrace(); return; } if (state.isopened()) { getdatafromfacebook(); } else if (state.isclosed()) { } }
what want achieve :
- login
- get basic data user (that part know how do)
- logout
after looking @ facebook developers guide couldn't find simple example appreciate example that.
i posting here simple class inside facebook examples:
public class loginusingactivityactivity extends activity { private static final string url_prefix_friends = "https://graph.facebook.com/me/friends?access_token="; private textview textinstructionsorlink; private button buttonloginlogout; private session.statuscallback statuscallback = new sessionstatuscallback(); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity); buttonloginlogout = (button)findviewbyid(r.id.buttonloginlogout); textinstructionsorlink = (textview)findviewbyid(r.id.instructionsorlink); settings.addloggingbehavior(loggingbehavior.include_access_tokens); session session = session.getactivesession(); if (session == null) { if (savedinstancestate != null) { session = session.restoresession(this, null, statuscallback, savedinstancestate); } if (session == null) { session = new session(this); } session.setactivesession(session); if (session.getstate().equals(sessionstate.created_token_loaded)) { session.openforread(new session.openrequest(this).setcallback(statuscallback)); } } updateview(); } @override public void onstart() { super.onstart(); session.getactivesession().addcallback(statuscallback); } @override public void onstop() { super.onstop(); session.getactivesession().removecallback(statuscallback); } @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); session.getactivesession().onactivityresult(this, requestcode, resultcode, data); } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); session session = session.getactivesession(); session.savesession(session, outstate); } private void updateview() { session session = session.getactivesession(); if (session.isopened()) { textinstructionsorlink.settext(url_prefix_friends + session.getaccesstoken()); buttonloginlogout.settext(r.string.logout); buttonloginlogout.setonclicklistener(new onclicklistener() { public void onclick(view view) { onclicklogout(); } }); } else { textinstructionsorlink.settext(r.string.instructions); buttonloginlogout.settext(r.string.login); buttonloginlogout.setonclicklistener(new onclicklistener() { public void onclick(view view) { onclicklogin(); } }); } } private void onclicklogin() { session session = session.getactivesession(); if (!session.isopened() && !session.isclosed()) { session.openforread(new session.openrequest(this).setcallback(statuscallback)); } else { session.openactivesession(this, true, statuscallback); } } private void onclicklogout() { session session = session.getactivesession(); if (!session.isclosed()) { session.closeandcleartokeninformation(); } } private class sessionstatuscallback implements session.statuscallback { @override public void call(session session, sessionstate state, exception exception) { updateview(); } } }
hope helps :)
Comments
Post a Comment