

Finally, you may find yourself wanting to change the look of your fragments depending on where they are located. The below image shows how two UI modules defined by fragments can be combined into one activity for a tablet design but separated for a handset design. To define a new fragment we either extend the class or one of its subclasses. The lifecycle of android fragment is like the activity lifecycle. For example, a transaction can add or change multiple fragments. That means you can re-use the same layout over and over again without having to rewrite code, or even show two different versions side-by-side.

OnResume() onPause()This is called when a fragemnt is no longer interactive and the user is about to leave the fragment. OnStart()This method is called when the fragment becomes visible on the device’s screen. By this time, we can even access the fragment’s view using the findViewById() method. OnActivit圜reated()This method is called when the host activity is created. This method usually returns a View component but if the fragment doesn’t have a UI, then you can return a null. It is called when the fragment is first created and then when the fragment returns back to the layout from the back stack. Dynamically Add Fragment in Android Using Android Studio.Fragment Tutorial With Example In Android Studio.
