The main building blocks of the Android app are application components. Each component is an entry point where the system or user can enter your application. Some components depend on others.
The application components are four different types:
Each type has a different purpose and has a separate lifecycle which defines how it is created and destroyed.
An activity is the point of entry for user interaction. A single display with a user interface. For example, an email app may have an activity that shows a new email list, another email - composing activity and another email - reading activity. While the activities work together to create a cohesive user experience within the email app, they are independent of each other. Therefore, if the email app allows you, a different application can start any of these activities. For example, a camera app can start the activity in the email app that composes new mail to allow the user to share a picture.
You implement an activity as a subclass of the Activity class For Example.
public class MainActivity extends Activity {
}
Another important component of an android application is service A service is a general-purpose entry point for keeping an app running in the background for all kinds of reasons. It is a component that runs behind the scenes for long term operations or for remote processes. A service does not provide a user interface. For example, while the user is in a different app, a service can play music background, or collect data from the network without blocking the user interaction.
As a subclass of service, a service is implemented.
public class MyService extends Service {
}
A receiver for broadcasting is an element which receives information, is broadcast on other requests or the system, and therefore takes some steps. For instance, an app can set an alarm to alert the user at any time. It’s same as we set an alarm to notify at a specific time. An additional example is to notify the user of low battery.Although the user doesn't display the UI, status bar notifications may be created to alert the user to a specific measure.
A recipient of the broadcast is implemented as a BroadcastReceiver subclass.
public class MyReceiver extends BroadcastReceiver {
public void onReceive(context,intent){}
}
A content provider manages a shared set of app data that you can store in the file system, in a SQLite database, on the web, or on any other persistent storage location that your app can access. Other applications can search or modify data via the content provider, if it is possible for the content provider.
Means it acts as bridge between the data and the application. You can also use content providers to access many native databases. For example, You can access contacts using ContactManager. You should be able to access other applications data correctly.
You can implement Content Providers by subclassing ContentProvider as below
public class MyProvider extends ContentProvider {
public void onCreate(){}
}
There are some additional components as well that are used with above fundamental components. They are –
A fragment is a part of user interface in a activity. In an activity, you can use a fragment or more. You can assume fragment as a modular part of activity that has it’s own life cycle, can receive it’s own input, can be added or removed dynamically, while activity is running. A Fragment must always be embedded in activity because it’s life cycle is affected by host activity life cycle. i.e. you can use fragment without activity.
Views
The user interface component is a fundamental building block. It is rectangular area on the screen. In this rectangular area we can draw or manage certain events. View is also a widget base class (used to ui component creation).
Layouts
Any user interface is designed with a visual structure. For example, UI for activity, widgets, fragments etc. You can declare layouts in 2 ways.
Statically : You can declare layouts in xml file. xml file is added in res => layout folder of any android application.
Dynamically : You can declare layouts at runtime as well. You will need to add related code to java file to create layouts at runtime.
Intent is to carry out an abstract information concerning an operation. Intent is used to communicate between different android components in several ways. Some of them are :
Start an Activity : You can use intent to start an activity from another activity or fragment class.
Start a Service : Same as Activity, You can use intent to start an service from a activity or fragment class.
Broadcasting a message : You can use intent to broadcast a message so that proper action is taken against the message broadcasted.
Before the Android system can start an app component, the system must know that the component exists by reading the app's manifest file, AndroidManifest.xml. Your application must declare all of its components to the root of the project directory of the application in this file.
The manifest does a number of things in addition to declaring the app's components, such as the following:
Identifies any user permissions the app requires, such as Internet access or read-access to the user's contacts.
Declares the minimum API Level required by the app, based on which APIs the app uses.
Declares the app's hardware and software features, such as a camera, bluetooth or a multitouch screen.
Declares API libraries the app needs to be linked against (other than the Android framework APIs), such as the Google Maps library.
When using the user to create an activity in Android applications, there are many more thing you need. You may want pictures, icons, animations, audio, video, etc. while creating user interface. They are the resources we need in the application somewhere. For these resources Android has a separate and structured folder. They are stored in any android application in the res folder.