Search code examples
androidstaticandroid-viewpagerfragmentpageradapterpagerslidingtabstrip

How can I access localised strings from within a static FragmentPagerAdapter


I have followed official documentation on how to implement swipe views with TabStrip instead of Tabs in order to create a Fragment (FragmentMyAccount.class) that contains Nested Fragments (FragmentMyProfile.class and FragmentMyLibrary.class). These nested fragments correspond to the two tabs managed by a childFragmentManager.

The two tabs and astuetz's pagerSlidingTabStrip work fine, but adapter's getPageTitle method doesn't have access to String resources and can only return a hardcoded string for each tab title (see on the bottom of the code). The compile-time error that appears when I try to access the xml resources reads:

"FragmentMyAccount.this cannot be referenced from a static context"

If the code below is not possible to tweak, what alternative implementation would be adequate to achieve my goal?

package com.kouretinho.myapp;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.astuetz.PagerSlidingTabStrip;


public class FragmentMyAccount extends Fragment {

private static final int NUM_ITEMS = 2;

MyAccountAdapter mAdapter;
ViewPager mPager;
PagerSlidingTabStrip mTabStrip;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_my_account, container, false);

    // Initialize the FragmentPager Adapter
    mAdapter = new MyAccountAdapter(getChildFragmentManager());

    // Initialize the ViewPager and set an adapter
    mPager = (ViewPager) rootView.findViewById(R.id.viewpager_my_account);
    mPager.setAdapter(mAdapter);

    // Initialize the TabStrip and bind the tabs to the ViewPager
    mTabStrip = (PagerSlidingTabStrip) rootView.findViewById(R.id.viewpager_tab_strip);
    mTabStrip.setViewPager(mPager);

    return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

public static class MyAccountAdapter extends FragmentPagerAdapter{

    private static final int MY_PROFILE = 0;
    private static final int MY_LIBRARY = 1;
    private static final String sExceptionMessage = "FragmentPagerAdapter was asked to getItem " +
            "with position other than 0 or 1. This FragmentPagerAdapter can only return one of" +
            "two Fragments at position 0 or 1.";

    public MyAccountAdapter(FragmentManager fm){
        super(fm);
    }

    @Override
    public int getCount() {
        return FragmentMyAccount.NUM_ITEMS;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case MY_PROFILE:
                return new UserProfileFragment();
            case MY_LIBRARY:
                return new UserBooksFragment();
            default:
                throw new IllegalStateException(sExceptionMessage);
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position){
            case MY_PROFILE:
// **********  THIS LINE IS PROBLEMATIC, cannot get Localised String resources
               return  FragmentMyAccount.this.getActivity().getResources().getString(R.id.my_profile);
            case MY_LIBRARY:
                return "my_library";
            default:
                return "no_title";
        }
    }
}

}


Solution

  • In your case you have to replace:

    public static class MyAccountAdapter extends FragmentPagerAdapter
    

    with

    public class MyAccountAdapter extends FragmentPagerAdapter
    

    Another way to fix this is to pass Context to MyAccountAdapter in constructor (like you do with FragmentManager).