Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Activity object from Custom Preferences

Tags:

android

I am trying to integrate facebook-connect to my android application. 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

like image 307
Thiago Avatar asked Sep 16 '11 14:09

Thiago


2 Answers

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;
like image 98
Camille Sévigny Avatar answered Oct 12 '22 22:10

Camille Sévigny


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;
}
like image 29
prom85 Avatar answered Oct 12 '22 22:10

prom85