Thiago
Thiago

Reputation: 4842

Get Activity object from Custom Preferences

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

Upvotes: 7

Views: 3381

Answers (3)

prom85
prom85

Reputation: 17878

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;
}

Upvotes: 4

Camille Sévigny
Camille Sévigny

Reputation: 5143

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;

Upvotes: 9

Richard Ev
Richard Ev

Reputation: 54167

Assuming you have an activity called MyActivity, can you just use MyActivity.class?

Upvotes: 0

Related Questions