Search code examples
androidandroid-fragmentsfragmentmanager

viewpager with images works but on back button images don't show


I have a viewpager with image fragments in it that works but on back was getting an error saying that there is no empty constructor. I made one but now my images don't show up because imageResourceId is null. I've tried a million things and can't figure it out.

public final class TestFragment extends Fragment {
String imageResourceId;

public TestFragment() {

}

public TestFragment(String cONTENT) {
    imageResourceId = cONTENT;
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    ImageView image = new ImageView(getActivity());
    new DownloadImageTask((ImageView) image).execute(imageResourceId);
    LinearLayout layout = new LinearLayout(getActivity());

    layout.setGravity(Gravity.CENTER);
    layout.addView(image);

    return layout;

}

 }





class TestFragmentAdapter extends FragmentPagerAdapter implements
    IconPagerAdapter {

public String[] CONTENT;
private int mCount;
protected static final int[] ICONS = new int[] {
        R.drawable.perm_group_calendar, R.drawable.perm_group_camera,
        R.drawable.perm_group_device_alarms, R.drawable.perm_group_location };

public TestFragmentAdapter(FragmentManager fm, JSONArray photoData) {
    super(fm);

//  photoData = (JSONArray) userProfile.get("photos");

    CONTENT = new String[photoData.length()];

    mCount = photoData.length();
    for (int i = 0; i < photoData.length(); i++) {
        JSONObject mainPhoto;
        try {
            mainPhoto = photoData.getJSONObject(i);
            CONTENT[i] = mainPhoto.getString("src_big");
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    notifyDataSetChanged();

}

@Override
public Fragment getItem(int position) {
    return new TestFragment(CONTENT[position]);
}

@Override
public int getCount() {
    return mCount;
}

@Override
public int getIconResId(int index) {
    return ICONS[index % ICONS.length];
}

public void setCount(int count) {
    if (count > 0 && count <= 10) {
        mCount = count;
        notifyDataSetChanged();
    }
}

in the activity

                    mAdapter = new TestFragmentAdapter(
                    getSupportFragmentManager(), photoData);
                    mPager = (ViewPager) findViewById(R.id.pager);
                    mPager.setAdapter(mAdapter);

                    mIndicator = (LinePageIndicator) findViewById(R.id.indicator);
                    mIndicator.setViewPager(mPager);

Solution

  • Android uses the empty constructor of a fragment to restore the fragment when there is a configuration change, that's why you can't modify it, and you can't override it because you get an error in eclipse. The best practice to pass arguments to a fragment, when instantiating it, is to use an static factory method:

    public class TestFragment extends Fragment {
    
        String imageResourceId;
    
        public static TestFragment newInstance(String resource) {
            TestFragment f = new TestFragment();
            Bundle args = new Bundle();
            args.putString("Resource", resource);
            f.setArguments(args);
            return f;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            imageResourceId = getArguments().getString("Resource");
        }
    
    }
    

    To instanciate it you use the static method:

    TestFragment fragment = TestFragment.newInstance("some string");