I have a main home activity which consists of 3 tabs at the bottom of the screen.
Inside this activity
I have a fragment
.
This fragment contains a view pager
. This view pager is used to navigate between 2 activities:
1.Exercise Form = This displays a video for the exercise.
2.Exercise History = This shows all of the users recorded data on the exercise.
To obtain the exercise form video as well as the exercise history, I need to be able to access the ExerciseName from ProgressFagment2 (The text at the top of the screen) and pass this to the 2 child fragments (ExerciseHomeVideoFragment and ExerciseHistoryFragment)
How can I access the exerciseName
in the child fragments?
ProgressFragment2 (Fragment Inside Activity)
public class Progress_Fragment2 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.exercises_history_demo, container, false);
}
public void onViewCreated(View view, Bundle savedInstanceState) {
childExerciseName = getArguments().getString("ExerciseName");
TextView title_tv = getView().findViewById(R.id.textView_exerciseName8);
title_tv.setText(childExerciseName);
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
ViewPager mViewPager = (ViewPager) getView().findViewById(R.id.view_pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) getView().findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.top_navigation_two, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ExerciseHomeVideoFragment();
break;
case 1:
fragment = new ExerciseHistoryFragment();
break;
}
return fragment;
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Exercise Form";
case 1:
return "Exercise History";
}
return null;
}
}
}
ExerciseHomeVideoFragment (One of the 2 fragments inside viewPager) |Relevant code only
public class ExerciseHomeVideoFragment extends Fragment implements View.OnClickListener {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_exercise_info_home_version, container, false);
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void onViewCreated(View view, Bundle savedInstanceState) {
Progress_Fragment2 parentFrag = ((Progress_Fragment2)ExerciseHomeVideoFragment.this.getParentFragment());
youTubeBtn = getView().findViewById(R.id.youTubeSearchButton);
parent_tv = getView().findViewById(R.id.textView_exerciseName8);
childExerciseViewModel = ViewModelProviders.of(this).get(ChildExerciseViewModel.class);
//
// HOW CAN I ACCESS THE CHILDEXERCISE NAME???
//
// childExerciseName = getArguments().getString("ExerciseName"); // THIS DOESNT WORK
// exerciseNameTV.setText(childExerciseName + " Demo");
}
}
you can send data using Interface, set argument or newInstance
in your tab fragment
switch (position) {
case 0:
fragment = new ExerciseHomeVideoFragment().newInstance(childExerciseName);
break;
case 1:
fragment = new ExerciseHomeVideoFragment();
break;
}
ExerciseHomeVideoFragment
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ExerciseHomeVideoFragment extends Fragment {
public static ExerciseHomeVideoFragment newInstance(String ExerciseName) {
Bundle args = new Bundle();
args.putString("ExerciseName", ExerciseName);
ExerciseHomeVideoFragment fragment = new ExerciseHomeVideoFragment();
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_exercise_info_home_version, container, false);
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void onViewCreated(View view, Bundle savedInstanceState) {
TextView title_tv = getView().findViewById(R.id.textView_exerciseName8);
//
Bundle bundle = getArguments();
if(bundle!=null)
{
title_tv.setText(bundle.getString("ExerciseName"));
}
}
}
you can send model also, but the model should be implemented using Serializer
switch (position) {
case 0:
fragment = new ExerciseHomeVideoFragment().newInstance(childExerciseViewModel);
break;
case 1:
fragment = new ExerciseHomeVideoFragment();
break;
}
in fragment
public static ExerciseHomeVideoFragment newInstance(String ExerciseName,ChildExerciseViewModel childExerciseViewModel) {
Bundle args = new Bundle();
args.putString("ExerciseName", ExerciseName);
args.putSerializable("model", childExerciseViewModel);
ExerciseHomeVideoFragment fragment = new ExerciseHomeVideoFragment();
fragment.setArguments(args);
return fragment;
}
Bundle bundle = getArguments();
if(bundle!=null)
{
title_tv.setText(bundle.getString("ExerciseName"));
ChildExerciseViewModel model = (ChildExerciseViewModel) bundle.getSerializable("model");
title_tv.setText(model.gettourExerciseText());
}