Dielson Sales
Dielson Sales

Reputation: 1737

Should you be checking if the fragment isAdded() whenever you call getParentFragment()?

The current version of Fragment deprecates getFragmentManager() in favor of getParentFragmentManager(), which is not nullable and throws an IllegalStateException instead. The same method says that you can check for isAdded() if you want to know whether the Fragment is currently added to an activity/context.

Does this mean that now you should be checking for isAdded() the same way you'd check if it's != null before you call getParentFragmentManager()?

Or is the IllegalStateException a hint that it shouldn't even be null if you call it during the normal cycle of the fragment (since it will only call onCreate() after onAttach()).

Upvotes: 1

Views: 305

Answers (1)

Kiryl Tkach
Kiryl Tkach

Reputation: 3634

Does this mean that now you should be checking for isAdded() the same way you'd check if it's != null before you call getParentFragmentManager()?

Looks like so. Before getFragmentManager() was nullable, so even if your call was completely safe (you called it after onAttach() and before onDetach()) the ide was showing you that this method can return null. Now you are responsible to call it only when you are sure that this fragment is added to parent fragment, and to check it you have the method isAdded().

Upvotes: 2

Related Questions