'Retained NexusDialog Fragment within a Fragment

I'm using Android Studio default implementation of AppCompatActivity which create 3 Fragments HomeFragment GalleryFragment and SlideSHowFragment.

In GalleryFragment Layout I have added a LinearLayout referenced by R.id.gallerylinearLayout

I have create a class MyFormFragment extending NexusDialog FormFragment Class following NexusDialog examples https://github.com/dkharrat/NexusDialog

Within GalleryFragment Class method onCreateView(@NonNull LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) I use the Fragment Manager to add MyFormFragment in case no retained version exists by adding the following code to the method :

    FragmentManager fm = getParentFragmentManager();
    MyFormFragment formFragment;

    Fragment retainedFragment = fm.findFragmentByTag(FORM_FRAGMENT_KEY);
    if (retainedFragment != null && retainedFragment instanceof SettingsFormFragment) {
        formFragment = (MyFormFragment) retainedFragment;
        
    } else {
       
        formFragment = new MyFormFragment();

        fm.beginTransaction()
                .add(R.id.gallerylinearLayout, formFragment, FORM_FRAGMENT_KEY) //R.id.linearlayout is in FragmentHome
                .commit();
      
    }

My question is what should I do when the retainedFragement exists to have it in the had hoc container referenced by R.id.gallerylinearLayout ? With the current code MyFormFragment show up correctly inside the galleryFragment when first created but if I navigate to another Tab (let's Say HomeFragment) and come back to Gallery Fragment it does not show up.

TIA



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source