tundundun
tundundun

Reputation: 644

Android replace fragment inside another fragment

I have 2 fragments inside parent fragment. In OnCreateView() of parent fragment I try to show these fragments via replace(int, Fragment). As I understand, FragmentTransaction#commit() is being called asynchronously.

So, if user quickly presses back button -- the system will destroy parent fragment (it calls onPause(), onStop(), onDestroyView(), onDestroy()) and asynchronous replacing task will fail -- parent view is destroyed and system throws "IllegalArgumentException: No view found for fragment MyFragment". I use support library.

So, how can I manage this case? I'll be highly appreciated for any help.

Upvotes: 2

Views: 548

Answers (1)

tundundun
tundundun

Reputation: 644

I solved my problem. For nested fragments getChildFragmentManager() should be used instead of getFragmentManager(). ChildFragmentManager appears to track a lifecycle of parent fragment and cancel scheduled commits if parent fragment is going to be destroyed.

Upvotes: 1

Related Questions