How do you pass data between Fragments
.
I've created a InstructionActivity
, InstructionsFragment
, StepsFragment
and SharedViewModel
. I'm trying to pass String from InstructionsFragment
to the StepsFragment
this the result in my logCat
:
D/StepsFragment:THIS com.shawn.nichol.bakingapp.Fragments.SharedViewModel@23e39c0
I have also put the whole code up on GitHub.
SharedViewModel:
public class SharedViewModel extends ViewModel {
private final MutableLiveData<String> stepPosition = new MutableLiveData<>();
public void setStepPosition(String position) {
stepPosition.setValue(position);
}
public MutableLiveData getStepPosition() {
return stepPosition;
}
}
InstructionsFragment:
public class InstructionsFragment extends Fragment {
private static final String LOGTAG = "InstructionsFragment";
private RecyclerView mRecyclerView;
private InstructionsAdapter mAdapter;
private String mAllIngredients;
private SharedViewModel model;
// Empty constructor
public InstructionsFragment() {
}
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle onSavedInstanceState) {
View view = inflater.inflate(R.layout.fragment_instructions, container, false);
model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
mRecyclerView = (RecyclerView) view.findViewById(R.id.ingredients_instructions_rv_fragments);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity().getApplicationContext(),
mRecyclerView, new RecyclerTouchListener.ClickListener() {
@Override
public void onClick(View view, int position) {
Log.d(LOGTAG, "Step " + (position + 1) + " " +
InstructionsExtractSteps.stepsShortDescriptionList.get(position));
model.setStepPosition("HELP");
FragmentManager mFragmentManager = getFragmentManager();
StepsFragment mStepsFragment = new StepsFragment();
FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction
.replace(R.id.instructions_place_holder, mStepsFragment)
// Puts InstructionsFragment on back stack, when back button is press it will
// reload that fragment instead of going back to the RecipeActivity.
.addToBackStack(null)
.commit();
}
@Override
public void onLongClick(View view, int position) {
}
}));
mAdapter = new InstructionsAdapter();
mRecyclerView.setAdapter(mAdapter);
return view;
}
/**
* onViewCreated: I found this to be the only way to update the TextView in fragment_instructions.xml
*
* @param view
* @param savedInstanceState
*/
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState){
TextView mLoadIngredients = (TextView)getView().findViewById(R.id.ingredients_tv_fragment);
// Set ingredients to screen
for(int i = 0; i < InstructionsExtractIngredients.ingredientList.size(); i++) {
if(i == 0) {
mAllIngredients = "- " + InstructionsExtractIngredients.ingredientList.get(i);
} else {
mAllIngredients = mAllIngredients + "\n - " + InstructionsExtractIngredients.ingredientList.get(i);
}
}
Log.d(LOGTAG, mAllIngredients);
mLoadIngredients.setText(mAllIngredients);
}
}
StepsFragment:
public class StepsFragment extends Fragment {
private static final String LOGTAG = "StepsFragment";
// Requires an empty constructor
public StepsFragment() {
}
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_step, container, false);
final SharedViewModel model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
model.getStepPosition();
Log.d(LOGTAG, "THIS " + model);
return view;
}
}