简体   繁体   中英

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?

It looks like they recognized this problem in the new Facebook SDK (v 4.0 onwards) and they now let you register a fragment on the LoginButton. Following is the code snippet from the official Facebook Login For Android guide.

 // If using in a fragment
    loginButton.setFragment(this);    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM