DEV Community

loading...

Basic concepts of Android-Part 1

Chetan
Learner
・4 min read

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

Package Name:

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

In the above code snippet:

  • com is the domain
  • google is the developer's company name
  • android.app.photos is the app name

Minimum SDK:

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
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.

Activity:

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

In the above image, we had setContentView method and in this, we had set R.layout.activity_main as the layout for this activity.

Activity Life Cycle:

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

  • onCreate()- This is the first callback and called when the activity is first created.
  • onStart()- This is called when the activity becomes visible to the user.
  • onResume()- This is called when the user starts interacting with the application.
  • onPause()- 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.
  • onStop()- This callback is called when the activity is no longer visible.
  • onDestroy()- This callback is called before the activity is destroyed by the system.
  • onRestart()- This callback is called when the activity restarts after stopping it.

Service:

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:

  • Started: 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.
  • Bound: 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.

image
While creating a service following are the most important callback methods that you should override:

  • onStartCommand()- 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.
  • onBind()- 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.
  • onUnbind()- The system calls this method when all clients have disconnected from a particular interface published by the service.
  • onRebind()- 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).
  • onCreate()- 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.
  • onDestroy()- 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.

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

Discussion (1)

Collapse
sinhasamarth profile image
Samarth Sinha

Nice 👍