Skip to content Skip to sidebar Skip to footer

Getcontext() And Getactivity() With Fragmentactivity

I need to use getActivity() and getContext() methods with FragmentActivity. How to make it? I can't extends Fragment class(i can't to do now). Maybe I can cast or something else. N

Solution 1:

getActivity() and getContext() are strictly Fragment methods. Since the FragmentActivity class extends the Activity class, the alternatives are 'this' and 'getApplicationContext()' respectively.

e.g.

mGoogleApiClient = new GoogleApiClient.Builder(this.getContext())

can become, simply

mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())

Solution 2:

You can't do that because a FragmentActivity is an Activity. So you can use this to access your context.

Use this:

this.getResources()

instead of

getContext.getResources()

Post a Comment for "Getcontext() And Getactivity() With Fragmentactivity"