Introduction to Activities in Android
Activity class is one of the very important parts of the Android component. Any app, don’t matter how much small it is (in terms of code and scalability), it has at least one Activity class. Unlike most of the programming languages, in which the main() method is the entry point for that program or application to start its execution, the android operating system initiates the code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle. So it can be said that An activity is the entry point for interacting with the user. Every activity contains the layout, which has a user interface to interact with the user. As we know that every activity contains a layout associated with it, so it can be said that activity class is the gateway, through which a user can interact programmatically with UI. Layout for a particular activity is set with the help of setContentView(). setContentView() is a function that takes View as a parameter. The view parameter basically contains the layout file for that activity. The following images indicate that activity_main is the layout file of MainActivity.
In the case of Java:
In the case of Kotlin:
While activities are often presented to the user as the full-screen window, Multiwindow mode, or Picture in Picture mode, here are two methods almost all subclasses of Activity will implement:
1. onCreate() Method
protected void onCreate(Bundle savedInstanceState)
- onCreate(Bundle) method is the place, where the user initializes the activity. This method is called when the activity is starting. This is the method that is used to initialize most of the things in the android app. onCreate() method takes savedInstanceState as the parameter, which the object of type Bundle, i.e Bundle Object which contains the previously saved data of the activity. If the activity is newly created then the bundle won’t be saving any data of the activity and would contain the null value.
- onCreate() method calls the setContentView() method to set the view corresponding to the activity. By default in any android application, setContentView point to activity_main.xml file, which is the layout file corresponding to MainActivity. The onCreate method uses the findViewById() method so that the user can interact programmatically with the widgets in android and then customize them according to need.
Bundle: If the activity is being re-initialized or restarted after previously being closed, then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). onSaveInstanceState() method is the method that is called to save the data just before our activity is killed.
2. onPause() Method
protected void onPause()
This method is called as part of the activity lifecycle when the user no longer actively interacts with the activity, but it is still visible on the screen. Let’s suppose the user is running two apps simultaneously on a mobile phone, i.e when activity B is launched in front of activity A, so activity A will go in the onPause() state, and activity B would go in the onStart() state of the activity lifecycle. One important point to be remembered is that for any activity to be accessed by system, ie android here, that activity must be declared in a manifest file. The manifest file is an XML file included in the application and default known as AndroidManifest.xml.
Declaring Activity in Manifest File
Open the app folder, and then open the subfolder manifest, and then open the AndroidManifest.xml file.
Let’s suppose the reader wants to have one more activity, apart from the MainActivity which is included by default in the project. Before adding one more activity and not doing any changes let’s see how the AndroidManifest.xml file looks like:
Now let’s add another activity named SecondActivity, and see how to declare it in the manifest file. One must write the declaration code within the application tag, otherwise, the declaration will give the error, and SecondActitvity will not be detected by the System. Declaration Code is given below.
So it can conclude that an application can have one or more activities without any restrictions. Every activity that the android app uses must be declared in the AndroidManifest.xml file. and the main activity for the app must be declared in the manifest with a <intent-filter> that includes the MAIN action and LAUNCHER. Any activity whether it is MainActivity or any other activity must be declared within the <application> of AndroidManifest file. If the user forgets to declare any of the activity, then android will not be able to detect that activity in the app. If either the MAIN action or LAUNCHER category is not declared for the main activity, then the app icon will not appear in the Home screen’s list of apps.
Adding Permissions to The App
One should remember that any service that the developer wants to use in the android app, like Internet service, Bluetooth service, Google Maps, App Notification service, etc, to use any of this service, the developer needs to take the permission of the android system. All those permission or request must be declared within the manifest file. Open the AndroidManifest.xml file, with the procedure shown above. Let say user need to add Internet permission, that needs to be accessed by the app. Add the below internet permission within the manifest tag.
Let say one need to add Bluetooth permission within the app, then it must be declared like this:
<uses-permission android:name=”android.permission.BLUETOOTH” />
Note: Conceptually It must be cleared that though the Activity class let’s say MainActivity extends AppCompatActivity in the code snippet given at the top, internally the structure is like shown in the image given below. It can be seen that AppCompatActivity too extends the Activity class.