0

Thank all in advance. And my problem is here: there is chain of elements

Activity has -> ViewPager has several -> Fragments have possibility call DialogFragment that can call startActivityForResult.

When onActivityResult called I need to restore ViewPager, and fragments in his adapter must be from restored supportFragmentManager fragments. I.e. for example in getItem I can't return some new Fragment, because DialogFragment was created in another fragment (that has restored) and it need to return some result to restored fragment.

  • It would be better to show the code of how you have setup the Activity, fragments and Adapter - https://stackoverflow.com/help/how-to-ask – Andrew Dec 23 '20 at 15:05

1 Answers1

0

If I understood you correctly you need to save your fragments like here:

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    if (adapter != null) {
        outState.putParcelable(SAVED_FRAGMENTS, adapter.saveState());
    }
    super.onSaveInstanceState(outState);
}

Then you can restore them in the following way:

 if (savedInstanceState != null && savedInstanceState.containsKey(SAVED_FRAGMENTS))   {
        adapter = new YourFragmentImplementation(getChildFragmentManager());
        adapter.restoreState(savedInstanceState.getParcelable(SAVED_FRAGMENTS), getContext().getClassLoader());
        Fragment restoredFragment = (Fragment) adapter.instantiateItem(viewPager, 0)
        // use restored fragment to add to adapter
 } else {
       // create a new fragment to add to adapter
 }
MPetrychko
  • 351
  • 1
  • 9