Skip to content Skip to sidebar Skip to footer

Issue With Fragment Transaction And Passing Values To Fragment?

im having some troubles with FragmentTransaction and passing values from activityA to fragmentA. The problem i think i am having is that i make 2 new FragmentA()instances and this

Solution 1:

There is a different approach to use to pass data from activity to bundle. Make a method newInstance(Objects you wanna pass as parameters) inside fragment, if you type in AndroidStudio newInstance inside Fragment class it will automatically suggest the implementation for you, if not it will look like this:

publicstatic TodoFragment newInstance(data to pass){
 Bundleargs=newBundle();
 //then you put into args the data you wann pass with args specific key,value method
 fragment.setArguments(args);
 return fragment;
}

In the activity instead of new TodoFragment(), you use TodoFragment.newInstance(data to pass); To retrieve the passed data you use getArguments(), in Fragment's onCreate()

Solution 2:

Maybe because the code is in a fragment's onCreate, I think it should be in onCreateView. Try in onCreate to retrieve the passed data only, and then in onCreateView write that code in an if bassed on the passed data not on the bundle.

Post a Comment for "Issue With Fragment Transaction And Passing Values To Fragment?"