Lets say I have a fragment inside that I have a view pager and in that view pager I am showing two other fragment (which shown in below code) which contains recyclerviews , now I am hosting the view pager fragment from an activity which is working fine.
public class MainScreenViewPagerFragment extends Fragment {
ViewPager mViewPager;
MembersNamesRecyclerViewFragment membersNamesRecyclerViewFragment;
AllMembersRecordsFragment allMembersRecordsFragment;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.entry_screen,container,false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setupViewPager(view);
}
private void setupViewPager(View view){
membersNamesRecyclerViewFragment = MembersNamesRecyclerViewFragment.newInstance();
allMembersRecordsFragment = AllMembersRecordsFragment.newInstance();
mViewPager = view.findViewById(R.id.list);
mViewPager.setAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
@NonNull
@Override
public Fragment getItem(int position) {
if(position == 0) {
if(membersNamesRecyclerViewFragment == null)
membersNameRecyclerViewFragment =
membersNameRecyclerViewFragment.newInstance() ;
return membersNameRecyclerViewFragment;
}
else {
if(allMembersRecordsFragment == null)
allMembersRecordsFragment =
allMembersRecordsFragment.newInstance();
return allMembersRecordsFragment;
}
}
@Override
public int getCount() {
return 2;
}
});
}
}
I just want understand how life cycle callbacks will work for the fragments which are inside view pager? are they too hosted by the activity? do the depend on the life cycle of the view pager fragment or the activity life cycle? who calls lifecycle methods on them?
there is no error in this code , i just want to understand if that approach is right, and the life cycle of memberNameRecyclerViewFragment and allMembersRecordsFragment