Get Activity object from Custom Preferences
I am trying to integrate facebook-connect to my android appli开发者_开发知识库cation. All the examples i am seeing over the internet are creating the connection from an Android activity. I am doing something a bit different, the user can configure its connection to facebook from a custom preference. I was successfull when doing it for twitter and foursquare. However, the method Facebook.authorize requires an Activity as parameter, and since i am inside a preference, i am not able to find any reference to an activity object.
So my question here is, how to get a reference for an activity inside a preference?
Thank you all T
I was able to get the Activity reference by casting the Context object to an Activity.
Activity activity = (Activity) context;
or with a custom activtity you can also do this
SettingsActivity activity = (SettingsActivity) context;
It's an old question, though I use following function with the com.android.support:preference
preferences fragment:
public static Activity getPrefActivity(Preference pref)
{
Context c = pref.getContext();
if (c instanceof ContextThemeWrapper)
{
if (((ContextThemeWrapper) c).getBaseContext() instanceof Activity)
return (Activity) ((ContextThemeWrapper) c).getBaseContext();
}
else if (c instanceof Activity)
return (Activity) c;
return null;
}
Assuming you have an activity called MyActivity
, can you just use MyActivity.class
?
精彩评论