Passing Different Images Every Second Fragment
Trying to pass dynamically different image every second Fragment and I have trouble to set it properly in onCreateView method and if statement. All images are stored in drawable fi
Solution 1:
If you want to pass data Fragment
from Activity
, try to use static static factory method as the following codes.
Fragment1
publicstatic Fragment newInstance(String str, int imageView , String[] rb, boolean arg) {
Fragmentfragment=newFragment1();
Bundleargs=newBundle();
args.putString("str", str);
args.putInt("image_resid", imageView);
args.putStringArray("rb", rb);
args.putBoolean("arg", arg);
fragment.setArguments(args);
return fragment;
}
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundleargs= getArguments();
stringValue = args.getString("str");
imagesResId = args.getInt("image_resid");
rbData = args.getStringArray("rb");
mapImage = args.getBoolean("arg");
}
MainActivity
fragmentList = new ArrayList<>();
fragmentList.add(Fragment1.newInstance(getResources().getString(R.string.text_page_1), R.drawable.swans, new String[]{getResources().getString(R.string.answer1), getResources().getString(R.string.answer2),getResources().getString(R.string.answer3)},false));
fragmentList.add(Fragment1.newInstance(null, R.drawable.image_file, null, true)); // TALKING ABOUT THIS LINE HERE AND LATER EVERY SECOND FRAGMENT. JUST IMAGE WILL CHANGE.
fragmentList.add(Fragment1.newInstance(getResources().getString(R.string.text_page_2), R.drawable.nature, new String[]{getResources().getString(R.string.answer4), getResources().getString(R.string.answer5),getResources().getString(R.string.answer6)},false));
Post a Comment for "Passing Different Images Every Second Fragment"