Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity and Fragment Lifecycle, does the onResume fire one after the other?

I am wondering what the proper order goes when a user returns to an Activity from the Home Screen.

Will the Activity's onResume() execute first, and then the Fragment's onResume() execute after? Does it do it in order, or will it do it concurrently?

I put some code in my Fragment's onResume() but it doesn't seem to be firing after the Activity's onResume().

like image 237
The Nomad Avatar asked Feb 27 '15 02:02

The Nomad


People also ask

Is the activity lifecycle connected with the fragment lifecycle?

A fragment life cycle is closely related to the lifecycle of its host activity which means when the activity is in the pause state, all the fragments available in the activity will also stop. Fragments added to the Android API in Android 3.0 which API version 11 to support flexible UI on large screens.

How does the lifecycle of fragments work?

Each Fragment instance has its own lifecycle. When a user navigates and interacts with your app, your fragments transition through various states in their lifecycle as they are added, removed, and enter or exit the screen.

Is onResume called after onCreate?

onResume() will never be called before onCreate() . Show activity on this post. onResume() will always be called when the activity goes into foreground, but it will never be executed before onCreate() .


2 Answers

I am wondering what the proper order goes when a user returns to an Activity from the Home Screen.

Activity's onResume() execute and the Fragment's onResume() execute after. Here is my screen shot of the log:

enter image description here

Will the Activity's onResume() execute first, and then the Fragment's onResume() execute after? Does it do it in order, or will it do it concurrently?

Yes, the two onResume() method execute in order, because they are all executed on the main thread(also called ui thread, it's a single thread), so there is no concurrent problem.

I put some code in my Fragment's onResume() but it doesn't seem to be firing after the Activity's onResume().

I think there is something wrong with your code, you can check it again or post it here, so someone may point your mistake.

like image 191
Lei Guo Avatar answered Oct 21 '22 19:10

Lei Guo


The fragments onResume() or onPause() will be called only after the Activities onResume() or onPause() is called. They are tightly coupled to the Activity.

Read the Fragment Lifecycle section of this article.

like image 45
januprasad Avatar answered Oct 21 '22 17:10

januprasad