Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between onResume() and onResumeFragments()

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?

like image 508
nekojsi Avatar asked Dec 23 '14 11:12

nekojsi


People also ask

What is the difference between onResume and onPostResume?

onPostResume() : protected void onPostResume() Called when activity resume is complete (after onResume() has been called). Applications will generally not implement this method; it is intended for system classes to do final setup after application resume code has run.

What is the fragment life cycle in Android?

A fragment can be used in multiple activities. Fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped. A fragment can implement a behaviour that has no user interface component.

How do you refresh a fragment on a resume?

The onResume() get called always before the fragment is displayed. Even when the fragment is created for the first time . So you can simply move your from onViewCreated() to onResume .

How do you write a fragment resume?

Since you want only one back stack entry per Fragment , make the back state name the Fragment's class name (via getClass(). getName() ). Then when replacing a Fragment , use the popBackStackImmediate() method. If it returns true, it means there is an instance of the Fragment in the back stack.


1 Answers

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.

like image 162
shkschneider Avatar answered Oct 02 '22 11:10

shkschneider