Header Ads Widget

Ticker

6/recent/ticker-posts

Android - Fragments

A Fragment is a piece of an activity which empower more measured activity design. It won't be right in the event that we say, a fragment is a sort of sub-activity.

 Following are significant focuses about fragment − 

A fragment has its own design and its own conduct with its own life cycle callbacks. 

You can add or eliminate fragments in an activity while the activity is running. 

You can join various fragments in a solitary activity to construct a multi-sheet UI. 

A fragment can be utilized in various activities. 

Fragment life cycle is firmly identified with the existence pattern of its host activity which implies when the activity is stopped, every one of the fragments accessible in the activity will likewise be halted. 

A fragment can execute a conduct that has no UI segment. 

Fragments were added to the Android API in Honeycomb version of Android which API version 11. 

You make fragments by broadening Fragment class and You can embed a fragment into your activity format by announcing the fragment in the activity's design document, as a <fragment> component. 

Preceding fragment presentation, we had a constraint since we can show just a solitary activity on the screen at one given point on schedule. So we couldn't partition gadget screen and control various parts independently. In any case, with the presentation of fragment we got greater adaptability and eliminated the limit of having a solitary activity on the screen at a time. Presently we can have a solitary activity yet every activity can contain numerous fragments which will have their own format, occasions and complete life cycle. 

Following is a run of the mill illustration of how two UI modules characterized by fragments can be joined into one activity for a tablet design, however isolated for a handset design.


The application can implant two fragments in Activity A, when running on a tablet-sized gadget. Be that as it may, on a handset-sized screen, there's not adequate space for the two fragments, so Activity An incorporates just the fragment for the rundown of articles, and when the client chooses an article, it begins Activity B, which incorporates the subsequent fragment to peruse the article.

Fragment Life Cycle

Android fragments have their own life cycle basically the same as an android activity. This part briefs various phases of its life cycle.

Here is the rundown of techniques which you can to override in your fragment class −

onAttach(): 


The fragment occasion is related with an activity instance.The fragment and the activity isn't completely instated. Commonly you get in this strategy a reference to the activity which utilizes the fragment for additional introduction work. 

onCreate()

 The framework calls this technique while making the fragment. You ought to introduce fundamental segments of the fragment that you need to hold when the fragment is stopped or halted, at that point continued. 

onCreateView() 

The framework calls this callback when it's the ideal opportunity for the fragment to draw its UI interestingly. To draw a UI for your fragment, you should return a View part from this technique that is the foundation of your fragment's design. You can return invalid if the fragment doesn't give a UI. 

onActivityCreated()

The onActivityCreated() is called after the onCreateView() technique when the host activity is made. Activity and fragment occurrence have been made just as the view chain of importance of the activity. Now, view can be gotten to with the findViewById() technique. model. In this technique you can start up objects which require a Context object 

onStart()

The onStart() strategy is called once the fragment gets noticeable. 

onResume()

Fragment gets dynamic. 

onPause()

 The framework considers this strategy as the principal sign that the client is leaving the fragment. This is normally where you ought to submit any progressions that ought to be persevered past the current client meeting. 


onStop()

Fragment going to be halted by calling onStop() 

onDestroyView()

Fragment view will annihilate after call this technique 

onDestroy()

onDestroy() called to do last tidy up of the fragment's state however Not destined to be called by the Android stage.

Post a Comment

0 Comments