Learn Android — Fundamentals

The world’s most popular mobile OS — from phones and watches to cars and TVs.

Akshansh Dhing
Parsed Inc.
6 min readJun 3, 2018

--

Welcome to the Learn Android series. I’m going to share with you, weekly, why and how to start with Android and build your portfolio. I’ll share with you explanations and code snippets on how to implement the most important and basic things in Android.

In the previous article, we learnt about how to create a new project and how to run it on a device. In this session, we’ll learn about the components that make up an application and dig deep into activity life cycles.

Previous Article: Learn Android — Getting Started

With respect to the software architecture, Android is based on a Linux Kernel that handles all the low level stuff like hardware and power management and drivers. Over this layer is the Android Runtime and the core libraries. On top of this is an Application Framework on which our instance of the application runs. And our application and all the others are present at the top most layer which is the Application Layer.

There are four main type of components that make up an Android application —

  1. Activities
  2. Services
  3. Content Providers
  4. Broadcast Receivers

All of these components are registered in the Android Manifest file. This can be thought of as a metadata (data about the data) file.

Activity

Is a single screen user interface which contains different components which can be interacted with. In simple words it can be described as a “single focused thing that a user can do”. When an application has more than one activity, then one of them should be labelled as the Launcher or the one that launches when application starts up. This labeling is done in the manifest file and can be shown as below.

Highlighted portion is responsible for making an activity a launcher activity.

For a user, an application can be thought of as a number of activities linked together to form an application. These activities are held on a stack and as we keep exiting one after the another, we are able to see the previous activities that we went through. If the stack becomes empty, we see the android home screen.

An activity has two main components — a Logic file (Java or Kotlin) and an User Interface file (XML). Separating out these helps keep the application easy to maintain and update. This also helps us to optimize our application for different device performances, languages and screen sizes.

Creating User Interface using XML.

Views

Are any rectangular object on the screen with varying attributes such as height, width, background color, etc. which displays some data or information content. These views make up the layout of the screen or User Interface. This UI can be broken down into it’s simplest bits — Views.

In Android, we use XML to describe how our layout will look like. This decision includes choosing which elements to place on the screen and where to place them on the screen. The layout files are in the app →res →layout directory of the project. Example of views include TextView (used to display text), ImageView (used to display image or icon), Button (sensitive to touch and performs assigned operations on click), and more.

XML is similar to HTML and uses tags to display elements on screen. Each of these tags have attributes related to it and some of them may contain other tags, exactly similar to how HTML works.

Layouts

Are a collection of views which define the UI. The views that contain and arrange other views to form a user interface are called View Groups or Container views. The most common View Groups are —

  1. LinearLayout — which arranges the components inside it in a linear fashion i.e. one after the another. This may be horizontally or vertically.
  2. ConstraintLayout — the newest on the block. This works by having components define a minimum of two constraints with respect to directions (top, right, bottom, and left).
  3. RelativeLayout — defining view position relative to other view components.
  4. ScrollView — allows content which is overflowing the screen to be seen by scrolling in the vertical direction.

If you look at the Java code of the activity. You’ll see that in the onCreate function inside of the activity, we use a function setContentView() and we pass in the name of the activity XML file as the parameter. This sets the layout to the particular activity.

You must be thinking what is the R.layout.name format. When the application is built and the code compiled, Android Studio generates Resource files which contains created constants that allow us to identify different contents of res folder. You can find more about resources here.

Classes

Java classes are where we include all the logic of our application into. The code below shows the raw state of the Java class made using EmptyActivity.

onCreate function is similar to main function in C++ or Java. It is called every time an activity is launched or resumed. So, we include all the code we need at the start of the activity into this function.

Lifecycle

To understand how an activity works, we need to understand the Activity life cycle states —

Activity life cycle diagram from Android Documentation.
  1. onCreate() — is called when the system first creates the activity. We include all the logic that needs to be run only once every time an activity is created in this function. We even set the content of the layout file in this function.
  2. onStart() — this is called after onCreate() function. This makes the activity visible to the user.
  3. onResume() — the activity is in this state before user performs any functions. It’s similar to an idle state and waiting for a user response to carry out a definite task.
  4. onPause() — this is called when the activity is not in the foreground. It pauses the operations that are performed only in the active state. It expects that the activity will once again go to the onResume() state.
  5. onStop() — is called when the activity is no longer visible to the user. It can be called when the activity has finished running, and is about to be terminated.
  6. onDestroy() — is called before the activity is destroyed. This even gets called when the screen orientation changes. The Android system cleans up all the resources and data.

For detailed information on the Activity life cycles, you can visit the documentation here.

The onCreate() function has a Bundle parameter passed. What is that? When we are interacting with the activity and for example we change the orientation of the activity. onDestroy() method will be called and all the progress that we’ve carried out in the activity will be destroyed. To make sure this isn’t the case the Android system saves all the state of the data into a Bundle named instanceState. So when the onCreate() is called again, this is passed so that when the activity is resumed, we haven’t lost any of the precious data that we were working with earlier.

Next up: Learn Android — Creating Layouts — 1

Stay tuned for weekly updates! Follow me and Parsed Inc. to never miss another update.

Also, let’s become friends on LinkedIn, GitHub, and Facebook!

To learn more about me and my work, visit my website!

Follow ParsedInc. on Facebook, LinkedIn, and Instagram!

If you enjoyed this article, feel free to 👏👏👏 a few times and share with a friend to help it reach someone who needs to read it. Thanks!

--

--