Bundle Null In Fragment
When i try to pass bundle variables to my Fragment in android they return as null in the fragment itself, yes I know this question has been asked a lot but this is my first 5 weeks
Solution 1:
Your problem is you are trying to manage Fragments in two different ways - manually and with a ViewPager. These are contradictory - choose one and only one.
You are setting the arguments Bundles in a transaction and then having the Adapter return Fragments that have no arguments set. To fix your problem, you will have to refactor your ViewPager method to set the arguments:
@Overridepublic Fragment getItem(int position) {
switch (position) {
case0:
Fragmentfrag=newHeroDescriptionFragment();
frag.setArguments(createBundle());
return frag;
case1:
Fragmentfrag=newHeroStoryFragment();
frag.setArguments(createBundle());
return frag;
default:
returnnull;
}
}
where createBundle()
a method for creating the bundle as in your original code.
Solution 2:
use a static method in your fragment class like this
publicstaticMyFragmentnewInstance(String data1,String data2){
MyFragment fragment=newMyFragment();
Bundle budle=newBundle();
bundle.putString("key",data1);
....
fragment.setArguments(bundle);
return fragment;
}
//then create new Fragment like thisMyFragment f=MyFragment.newInstance("a","b");
return f;
Post a Comment for "Bundle Null In Fragment"