All about Fragments in Android

Fragments are great for providing your app with UI flexibility. Suppose we have a simple app which shows a list of items. When clicking on an item in the list, we would like to present the user with more details about said item. This diagram shows how we would like our app to behave: As a beginner, you may assume that the best way to go about doing this would be to use two activities. One for showing the list and another for showing details when the user clicks on an item. While this solution works, it is not flexible in any sense of the word. What happens when we decide to display this information on a larger device? If we were to restrict ourselves to only using activities, each Activity  UI would take up the entire screen. This is not a good use of our available screen space. Doing this would make our app would feel barebones and empty. Luckily for us, there is a way to show multiple (or single) UIs in our Activity. This is where Fragments come in to play. A Fragment  in Android is essentially an individual part, or portion, of a UI. Building and combining multiple fragments allows us to compose them in an Activity  to create multi-pane user interfaces. Fragments are essential when it comes to following the master/detail design concept, as shown in the diagram above. Like Activities, Fragments have their own layouts and life cycle methods. However, unlike Activities, the Android OS does not have any knowledge of an Activity’s  Fragment . Its life cycle methods are called and managed by the Activity  which hosts the Fragment (s). Creating our Fragment class First, let’s take a look as to how we actually make a Fragment  class. For starters, we have to manually inflate the …

The Activity Lifecycle

The Activity lifecycle is one of the fundamental concepts of Android development you must familiarize yourself with as a beginner in Android development. You’re  probably asking yourself, what is this lifecycle and why is it important? Well, once you understand how state is managed in Activities, you will (hopefully!) never come face to face with the commonly dreaded screen “rotation problem” that exists in Android. There exists four different states an Activity can be in: resumed, paused, stopped or non-existent (not in memory). With these states, there are different lifecycle methods which notify the Activity of a change in state. Activity Life Cycle Methods onCreate()  is called when an Activity is being created. Most of the time, this is where an Activity’s UI is prepared. Activity exists in memory, but is not visible in any way. onStart() is called after onCreate() . This is where the Activity is about to become visible, but not in focus. Activity exists in memory, is in background. onResume()  is called when the Activity comes in to the foreground and has focus. Activity exists in memory, is in the foreground (visible). onPause()  is called when the Activity is in the background and is still visible to the user, but no longer has focus. Activity exists in memory, is in background  (visible). onStop()  is called when the Activity is no longer visible to the user, but is still in memory. Activity exists in memory, but is no longer visible. onDestroy()  is called when the Activity is about to be removed from memory. Activity is no longer visible and will be removed from memory. Handling Rotation Changes An Activity in Android goes through many different Activity lifecycle methods. However, once you rotate the device, or if you prefer the more technical term, make the Activity go through a configuration change, …