Home » Android » android – How to call getBaseContext() on Fragment

android – How to call getBaseContext() on Fragment

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have this method which can be called from normal Activity:

getBaseContext().getResources().updateConfiguration(config2, getBaseContext().getResources().getDisplayMetrics());

The problem is I cannot call getBaseContext() in Fragment, how can I to do that? Or with what do I need to replace the getBaseContext() from a Fragment class?

How to&Answers:

You can use getActivity(), which returns the activity associated with a fragment.
The activity is a context (since Activity extends Context).

But you can use it after the fragment is attached to the activity..

Answer:

use getActivity().getBaseContext();