android - Facebook Login from Nested Fragment not working - Activity result fragment index out of range -
my app structure follows
mainactivity(a viewpager contains 2 fragments - fragment 1 , fragment 2)
| |--fragment 1 | (viewpager several fragments | app tutorial/intro) | |--fragment 2 (single fragment have custom facebook login button)
at first, onactivityresult nested fragment not getting called. found solution using following code
@override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); log.d(tag, "onactivityresult"); list<fragment> frags = getsupportfragmentmanager().getfragments(); if (frags != null) { (fragment f : frags) { if (f != null) handleresult(f, requestcode, resultcode, data); } } } private void handleresult(fragment frag, int requestcode, int resultcode, intent data) { if (frag instanceof loginfragment) { // custom interface no signitures frag.onactivityresult(requestcode, resultcode, data); } list<fragment> frags = frag.getchildfragmentmanager().getfragments(); if (frags != null) { (fragment f : frags) { if (f != null) handleresult(f, requestcode, resultcode, data); } } }
now onactivityresult in loginfragment called, not login. onactivityresult loginfragment
@override public void onactivityresult(int requestcode, int resultcode, intent data) { log.d(tag, "loginfragment onactivityresult"); callbackmanager.onactivityresult(requestcode, resultcode, data); }
i have registered callback using
loginmanager.getinstance().registercallback onsuccess callback not hit.
instead log stating following
w/fragmentactivity﹕ activity result fragment index out of range: 0x2face d/mainactivity﹕ onactivityresult d/loginfragment﹕ loginfragment onactivityresult
how make sure login happens , callbacks work?
edit : on debugging issue, kept break point in onactivityresult callback of fragmentactivity class.
if (mfragments.mactive == null || index < 0 || index >= mfragments.mactive.size()) { log.w(tag, "activity result fragment index out of range: 0x" + integer.tohexstring(requestcode)); return; }
mfragments.mactive == null false.
index< 0 false
index >= mfragments.mactive.size() true
i figured out.
i trying login nested fragment using
loginmanager.getinstance().loginwithreadpermissions(this,arrays.aslist ("public_profile", "user_friends"));
instead, needed pass activity instead of fragment loginwithreadpermissions method.
therefore, correct way login in case is
loginmanager.getinstance().loginwithreadpermissions(getactivity(), arrays.aslist("public_profile", "user_friends"));
Comments
Post a Comment