Difference between onResume() and onResumeFragments()

nekojsi picture nekojsi · Dec 23, 2014 · Viewed 10.9k times · Source

FragmentActivity.onResume() javadoc:

Dispatch onResume() to fragments. Note that for better inter-operation with older versions of the platform, at the point of this call the fragments attached to the activity are not resumed. This means that in some cases the previous state may still be saved, not allowing fragment transactions that modify the state. To correctly interact with fragments in their proper state, you should instead override onResumeFragments().

FragmentActivity.onResumeFragments() javadoc:

This is the fragment-orientated version of onResume() that you can override to perform operations in the Activity at the same point where its fragments are resumed. Be sure to always call through to the super-class.

Does the above mean that the platform guarantees that:

  • fragments are never going to be resumed (their onResume() not called) while executing FragmentActivity.onResume() and
  • fragments are always going to be resumed (their onResume() called) while executing FragmentActivity.onResumeFragments()?

If not, how can a developer correctly utilize and be vigilant regarding the above?

Answer

shkschneider picture shkschneider · Dec 24, 2014

Will onResume() be called?

Yes, FragmentActivity.onResume() will still be called (same context as Activity.onResume()). Even if you override FragmentActivity.onResumeFragments() (additional method from FragmentActivity that knows it contains Fragments).

What is the difference between onResume() and onResumeFragments()?

FragmentActivity.onResumeFragments() is a callback on FragmentActivity as to when the Fragments it contains are resuming, which is not the same as when the Activity resumes.

This is the fragment-orientated version of onResume() that you can override to perform operations in the Activity at the same point where its fragments are resumed. Be sure to always call through to the super-class.

When to use which method?

If you are using the support-v4 library and FragmentActivity, try to always use onResumeFragments() instead of onResume() in your FragmentActivity implementations.

FragmentActivity#onResume() documentation:

To correctly interact with fragments in their proper state, you should instead override onResumeFragments().

Difference is subtle, see https://github.com/xxv/android-lifecycle/issues/8:

onResume() should be used for normal Activity's and onResumeFragments() when using the v4 compat library. This is only required when the application is waiting for the initial FragmentTransaction's to be completed by the FragmentManager.