How can I learn Android? |Day 2 Android four components and activity

AppGallery Team
4 min readFeb 1, 2021

Hello, everyone. Yeah, it’s me again. Last time I shared

I have to say, learning these concepts is really boring. To this anguish, I visited a colleague who could program. He told me that it would be easier to understand the four components from the activity perspective and to understand them in depth with some demos.

First, we need to have some basic Java knowledge before we understand the four components.

Java is an object-oriented programming language.

Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a person is an object. A person has attributes, which are fields in a class, such as age and name, and methods, such as eat and sleep.

A Class is like an object constructor or a “blueprint” for creating objects.

Constructors

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object fields.

Note that the constructor name must match the class name, and it cannot have a return type (like void).

Also, note that the constructor is called when the object is created.

All classes have constructors by default: if you do not create a class constructor yourself, Java creates one for you. However, then you are not able to set initial values for object fields.

Interfaces

Another way to achieve abstraction in Java is with interfaces.

An interface is a completely "abstract class" that is used to group related methods with empty bodies.

Notes on Interfaces:

Interface methods do not have a body — the body is provided by the “implement” class

On implementation of an interface, you must override all of its methods

Interface methods are by default abstract and public

An interface cannot contain a constructor (as it cannot be used to create objects)

Why And When To Use Interfaces?

1) To achieve security — hide certain details and only show the important details of an object (interface).

2) Java does not support “multiple inheritance” (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).

New

In Java, an object is created from a class. We have already created the class named Person, so now we can use this to create objects.

To create an object of Person , specify the class name, followed by the object name, and use the keyword new:

Now, let’s start with the four components.

The Activity class is a crucial component of an Android app and the way activities are launched and put together is a fundamental part of the platform’s application model.

The demo below would help us a better understanding.

The view is an internal component of an activity. A layout is a container that controls the orderly placement of these controls on the interface. Common layouts include linear layout, relative layout and etc. Common controls include TextView, ImageView, Button and etc.

An Android app typically has several activities. Each activity displays a user interface that allows the user to perform a specific task (such as view a map or take a photo). To take the user from one activity to another, your app must use an Intent to define your app’s “intent” to do something. When you pass an Intent to the system with a method such as startActivity(), the system uses the Intent to identify and start the appropriate app component. Using intents even allows your app to start an activity that is contained in a separate app.

An Intent can be explicit in order to start a specific component (a specific Activity instance) or implicit in order to start any component that can handle the intended action (such as “capture a photo”).

As the demo shows that the Intent is used to switch between two activities.

--

--

AppGallery Team

Insights, success stories, and monetization tips for app development at https://medium.com/appgallery