vendredi 27 mars 2015

Forwarding onActivityResult from Activity to Fragment in android

I am trying to implement Facebook login for my android app. However, instead of using the LoginActivity, as suggested on the developer website, I have added the Facebook login button on a separate fragment. This fragment simply holds the button and does all the processing, which otherwise the LoginActivity would have done. The purpose of using a separate fragment is to prevent doing everything in the LoginActivity, specially if other login options like Google+ also exist.


Now the problem is that the UiLifecycleHelper constructor accepts only Activity object for callbacks:



UiLifecycleHelper(Activity, StatusCallback)


Because of this, I am bound to do something like:



UiLifecycleHelper(getActivity(), callback)


So, eventually:



@override
onActivityResult(...)


is called in LoginActivity, instead of in my fragment which hosts the login button.


Now the authentication won't work, unless I explicitly call onActivityResult method in the fragment from onActivityResult from the LoginActivity. Because it is the fragment who has created and is maintaining the UILifecycleHelper object.


I want to avoid adding this "hack" in the LoginActivity, specially since, there's no way for me to know if the onActivityResult was called as a result of Facebook authentication process.


What's a proper way to achieve this?


Aucun commentaire:

Enregistrer un commentaire