Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity vs Fragment Lifecycle [closed]

Tags:

I am working on new application where I am using Activity and Fragment. Any main difference between them ??

Update

I found good answer which I wanted in Android docs.

Docs described

The most significant difference in lifecycle between an activity and a fragment is how one is stored in its respective back stack. An activity is placed into a back stack of activities that's managed by the system when it's stopped, by default (so that the user can navigate back to it with the Back button, as discussed in Tasks and Back Stack). However, a fragment is placed into a back stack managed by the host activity only when you explicitly request that the instance be saved by calling addToBackStack() during a transaction that removes the fragment.

Does host Activity keep different back stack of different Fragment associated with it and any scenario where your single application keep multiple stacks. ??

like image 679
N Sharma Avatar asked Apr 11 '14 05:04

N Sharma


People also ask

What happens to fragment when activity is stopped?

A paused Fragment is still alive (all state and member information is retained by the system), but it will be destroyed if the Activity is destroyed. If the user presses the Back button and the Fragment is returned from the back stack, the lifecycle resumes with the onCreateView() callback.

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.


2 Answers

 Differences between Activity and Fragment lifecyle in Android

Fragment is a part of an activity, which contributes its own UI to that activity. Fragment can be thought like a sub activity. Fragments are used to efficiently use the space in wider screen devices.

An activity may contain 0 or multiple number of fragments based on the screen size. A fragment can be reused in multiple activities, so it acts like a reusable component in activities.

A fragment can't exist independently. It should be always part of an activity. Where as activity can exist with out any fragment in it.

A fragment lifecycle is more complex than the activity lifecycle because it has more states. Lifecycle states are shown below:

enter image description here

onInflate

At very beginning of the fragment life the method onInflate is called. In this method we can save some configuration parameter and some attributes define in the XML layout file.

onAttach

After this step onAttach is called. This method is called as soon as the fragment is “attached” to the “father” activity and we can this method to store the reference about the activity.

onCreate

It is one of the most important step, our fragment is in the creation process. This method can be used to start some thread to retrieve data information, maybe from a remote server. The onCreateView is the method called when the fragment has to create its view hierarchy.During this method we will inflate our layout inside the fragment.

During this phase we can’t be sure that our activity is still created so we can’t count on it for some operation. We get notified when the “father” activity is created and ready in the onActivityCreated.

From now on, our activity is active and created and we can use it when we need.

onStart

The next step is onStart method. Here we do the common things as in the activity onStart, during this phase our fragment is visible but it isn’t still interacting with the user.

onResume

When the fragment is ready to interact with user onResume is called.

Then it can happen that the activity is paused and so the activity’s onPause is called. Well onPause fragment method is called too.

After it, it can happen that the OS decides to destroy our fragment view and so onDestroyView is called. After it, if the system decides to dismiss our fragment it calls onDestroy method.

Here we should release all the connection active and so on because our fragment is close to die. Even if it is during the destroy phase it is still attached to the father activity. The last step is detach the fragment from the activity and it happens when onDetach is called.

I hope you can understand from this.

Thanks.

like image 100
GrIsHu Avatar answered Sep 27 '22 21:09

GrIsHu


Directly from developer Fragments guide -

The lifecycle of the activity in which the fragment lives directly affects the lifecycle of the fragment, such that each lifecycle callback for the activity results in a similar callback for each fragment.

For example, when the activity receives onPause(), each fragment in the activity receives onPause().

Fragments have a few extra lifecycle callbacks, however, that handle unique interaction with the activity in order to perform actions such as build and destroy the fragment's UI. These additional callback methods are:

1) onAttach() =>

Called when the fragment has been associated with the activity (the Activity is passed in here).

2) onCreateView() =>

Called to create the view hierarchy associated with the fragment.

3) onActivityCreated() =>

Called when the activity's onCreate() method has returned.

4) onDestroyView() =>

Called when the view hierarchy associated with the fragment is being removed.

5) onDetach() =>

Called when the fragment is being disassociated from the activity.

Once the activity reaches the resumed state, you can freely add and remove fragments to the activity. Thus, only while the activity is in the resumed state can the lifecycle of a fragment change independently.

However, when the activity leaves the resumed state, the fragment again is pushed through its lifecycle by the activity.

Check Activity and Fragments.

like image 27
My God Avatar answered Sep 27 '22 21:09

My God