Android components are the main part of the application flow through which the application can able to do the basic functionalities. Each component has its own definition. Components mostly help the user to enter the app. Each component should define in the Manifest file. We will discuss everything about manifest files in a different session. Android has four types of components. These are
Activity is nothing but the displaying screens or pages when we are opening any app. It has its own way of flow which called Life Cycle. Just think like a song in which you are going to listen. I am providing the simple steps and we will compare it with the Activity life cycle. Just assume that the song is nothing but the Activity.
Click the Play button and the song will start. So in the same way call onCreate() method and the Activity will start now.
Now the song is playing that means it’s in resume state. Same the Activity is in resume state when it continues and the method which handling this state is called onResume().
If you want to pause the song then you should have to click the pause button in the media player. Same applies to the Activity as well and the method called onPause().
Again if you want to continue the song, you have to click the resume button and the song will come to resume state and continue playing. The same is for Activity. It goes to onPause() and again comes to onResume().
And at last, the song will finish and stop. In Activity, the method called onStop when Activity stopped.
If you want then you can replay the song, but the media player should now be stopped. Same in the Activity life cycle, it can be restarted but the app should not be closed. The method called onRestart() and then it goes to onResume().
But when we stopped the media player itself, the song totally cleared or we can say destroyed. You have to again open the media player then select the song to play again. It is exactly the same in Activity. When the user closes the application, the Activity stopped and destroyed and the method called onDestroy().
So we can say the basic flow will be like onCreate() — onStart() — onResume() — onPause() — onStop() — onDestroy().
And for restart, it will be like onCreate() — onStart() — onResume() — onPause() — onStop() — onRestart() — onStart() — onResume()
Service is nothing but a continuous flow that runs in the background. It needs when we want to forcefully continue something still user closes our application. You can say the same media player. Its simply plays and never bother whether the user opens or closes the app. Because we bound it with a background service and give the privilege to continue till user stops it. Its mostly two types. One is bound service and other one is unbound service. We will explain each area when we will discuss service briefly. The below images show both flows. It is quite the same as the Activity life cycle.
Broadcast Receiver (Some say Broadcast Listener)
What is BroadcastReceiver and What it does:
There are some system events in Android which helps the user to know about the system changes. These are like phone boot, getting phone message, change in the network, battery low, etc. And BroadcastReceiver only does the work to receive messages from those events. So the user can able to know about the system event changes. We will broadly discuss everything about BroadastReceiver, how it registers, how it helps, and how to use it in our program in a recent session.
What is ContentProvider:
A nice explanation I got from one Q/A post that A ContentProvider manages access to a structured set of data. It encapsulates the data and provides mechanisms for defining data security. ContentProvider is the standard interface that connects data in one process with code running in another process. For a better understanding purpose A Content Provider is working like a DataBase which can able to share its data in between Applications. For example Contact is a Content Provider. You can see many apps are using your phone contacts because it’s sharable. We will also discuss the Content Provider briefly in the near future.