0

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

Abhinav Chauhan
  • 1,304
  • 1
  • 7
  • 24
  • 1
    See #3 in https://proandroiddev.com/the-seven-actually-10-cardinal-sins-of-android-development-491d2f64c8e0 this will not work correctly (your `membersNamesRecyclerViewFragment` and `allMembersRecordsFragment` will differ from the one inside your ViewPager) – EpicPandaForce Mar 17 '20 at 13:08
  • but they are working correctly , i just wanted to understand the lifecycle , for which one the life cycler methods will be called first – Abhinav Chauhan Mar 17 '20 at 13:32
  • 1
    it looks like it is working correctly but you will have problems in production see https://stackoverflow.com/questions/55753099/app-crash-after-activity-has-been-killed-in-background/55754360#55754360 – EpicPandaForce Mar 17 '20 at 13:39
  • hey that was a very nice article , i was making some of those sins, thanks, can you suggest me where can i learn best practices – Abhinav Chauhan Mar 17 '20 at 13:48
  • so how to i use fragments inside viewpager i need to do that – Abhinav Chauhan Mar 17 '20 at 13:49
  • if i create fragment in only getItem() if they are not null will it work?, like i did in the edit of question – Abhinav Chauhan Mar 17 '20 at 13:51
  • You need to create the Fragments in `getItem()`, but not store the Reference as it will Not be invoked after the process is killed/recreated (so then Fragment ref would be null). See https://stackoverflow.com/questions/54279509/how-to-get-elements-of-fragments-created-by-viewpager-in-mainactivity/54280113#54280113 how to get Fragment instance from ViewPager in reliable way (after it has been created at least once by the FragmentPagerAdapter). – EpicPandaForce Mar 17 '20 at 14:40

0 Answers0