Basic concepts of Android-Part 1
Tags: android, computerscience, programming, devops
Published: 2021-04-21 11:23:40

In this article, we are going to discuss some concepts of Android that you might face in your interview or during the project.

<h2>Package Name:</h2>
The package name is used for unique identification for your application.
Android uses the package name to determine if the application has been installed or not. Generally, the package name of an app is in the format
```
domian.company.appname
```
Here domain portion is the domain extension like .com, .org, or .eu. The company portion is generally the name of the developer's company and appname describe the app itself. Appname could be of a single word or multiple words separated by a dot. For example
```
com.google.android.apps.photos
```
In the above code snippet:
<ul>
<li>com is the domain</li>
<li>google is the developer's company name</li>
<li>android.app.photos is the app name</li>
</ul>

<h2>Minimum SDK:</h2>
It is an integer that defines the minimum API level(version of the Android operating system) required to run your application. If the system's API Level is lower than the value specified in this attribute, then the Android system will prevent the user from installing the application.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/in0mcpfl9ijq8cqgk85u.png) 
In the above image, we had selected the Minimum SDK as 27 and Android Studio shows us that it will run on 53.5% of devices.

<h2>Activity:</h2>
Activity class is one of the most important parts of Android. No matter how small an app is, it will have at least an Activity. An Android activity is one screen of the Android app's user interface. An app can have multiple activities means multiple screens. Android activity is the subclass of ContextThemeWrapper class. 
Every activity contains a layout, which has a user interface to interact with the user. In this layout, we define what a user will see when (s)he open this activity and in Java/Kotlin we define the functionality of the activity.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obip7fdtkg1vsahgf8lj.png)
 
In the above image, we had setContentView method and in this, we had set R.layout.activity_main as the layout for this activity.

<h5>Activity Life Cycle:</h5>
Unlike other programming languages where **main()** method is the entry point of the program, the android operating system initiates the code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle. In the below diagram we can see the life cycle of an Activity.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6rcpidtoq70hlawcodk.png)
 
<ul>
<li><b>onCreate()-</b> This is the first callback and called when the activity is first created.</li>
<li><b>onStart()-</b> This is called when the activity becomes visible to the user.</li>
<li><b>onResume()-</b> This is called when the user starts interacting with the application.</li>
<li><b>onPause()-</b> The paused activity does not receive user input and cannot execute any code and called when the current activity is being paused and the previous activity is being resumed.</li>
<li><b>onStop()-</b> This callback is called when the activity is no longer visible.</li>
<li><b>onDestroy()-</b> This callback is called before the activity is destroyed by the system.</li>
<li><b>onRestart()-</b> This callback is called when the activity restarts after stopping it.</li>
</ul>

<h2>Service:</h2>
A service is a component that runs in the background to perform long-running operations such as playing music, handle network transactions, interacting with content providers without needing to interact with the user and it works even if the application is destroyed. It doesn't have any UI (user interface). The android.app.Service is a subclass of ContextWrapper class.

A service can essentially take two states:
<ul>
<li><b>Started:</b> A service is started when a component (like activity) calls the startService() method, now it runs in the background indefinitely. It is stopped by the stopService() method. The service can stop itself by calling the stopSelf() method.</li>

<li><b>Bound:</b> A service is bound when another component (e.g. client) calls the bindService() method. A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). The client can unbind the service by calling the unbindService() method.</li>
</ul>
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbkl0kf57nzq3i23077s.png)
 While creating a service following are the most important callback methods that you should override:
<ul>
<li><b>onStartCommand()-</b> The system calls this method when another component, such as an activity, requests that the service be started, by invoking startService(). When this method executes, the service is started and can run in the background indefinitely. If you implement make sure to stop the service when its work is done, by calling stopSelf() or stopService() methods.</li>
<li><b>onBind()-</b> The system calls this method when another component wants to bind with the service by calling bindService(). If you implement this method, you must provide an interface that clients use to communicate with the service, by returning an IBinder object. You must always implement this method, but if you don't want to allow binding, then you should return null.
<li><b>onUnbind()-</b> The system calls this method when all clients have disconnected from a particular interface published by the service.</li>
<li><b>onRebind()-</b> The system calls this method when new clients have connected to the service after it had previously been notified that all had disconnected in its onUnbind(Intent).</li>
<li><b>onCreate()-</b> The system calls this method when the service is first created using onStartCommand() or onBind(). This call is required to perform a one-time set-up.</li>
<li><b>onDestroy()-</b> The system calls this method when the service is no longer used and is being destroyed. Your service should implement this to clean up any resources such as threads, registered listeners, receivers, etc.</li>
</ul>

We will continue this in the next article.
Happy Learning!