Home » Android » resources – Access to getString() in android.support.v4.app.FragmentPagerAdapter?

resources – Access to getString() in android.support.v4.app.FragmentPagerAdapter?

Posted by: admin June 15, 2020 Leave a comment


In a class extending android.support.v4.app.FragmentPagerAdapter, is there any way to get access to the Context.getString(..) method without the extending class being an inner class of an activity or passing in some context from the outside?

Thanks for any hint!

How to&Answers:

From a fragment use :


From an adapter use :


Yes, you need a context to access the resources.


From an Activity, use:


From a Fragment, use:


From an adapter, use:



I had a similar issue. From a drawer layout, I wanted to decide which fragment to use in a method called from a helper class.

So in onCreateView…

String form = getResources().getStringArray(R.array.drawer_array)[i];
        Context context = getActivity().getApplicationContext();

        FragmentHelper fh = new FragmentHelper();
        int myFragment = fh.getCurrentFragment(form,context);

And in public FragmentHelper()…

    public int getCurrentFragment(String form, Context context){

The trick being to add context in front of getResources(). Otherwise, my stack showed that the fragment was not attached to an activity.

Hope this helps someone.