Introduction to Activities in Android
Activity class is one of the very important parts of the Android Component. Any app, don’t matter how small it is (in terms of code and scalability), has at least one Activity class. Unlike most 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 the activity class is the gateway, through which a user can interact programmatically with the 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 code has been given in both Java and Kotlin Programming Language for Android.
The Following Code Indicates that activity_main is the Layout File of MainActivity
Kotlin
import androidx.appcompat.app.AppCompatActivity import android.os.Bundle class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) } } |
Java
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } |
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:
- onCreate()
- onPause()
1. onCreate() Method
The syntax for Kotlin:
override fun onCreate(savedInstanceState: Bundle?)
The syntax for Java:
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
The syntax for Kotlin:
override fun onPause() { super.onPause() }
The syntax for Java:
protected void onPause() { super.onPause(); }
This method is called 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, 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 a system, i.e. android here, that activity must be declared in a Manifest File. The Manifest File is an XML file included in the Application and is by 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 what the AndroidManifest.xml File looks like
XML
<? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" package = "com.gfg.exampleactivity" > < application android:allowBackup = "true" android:dataExtractionRules = "@xml/data_extraction_rules" android:fullBackupContent = "@xml/backup_rules" android:icon = "@mipmap/ic_launcher" android:label = "@string/app_name" android:roundIcon = "@mipmap/ic_launcher_round" android:supportsRtl = "true" android:theme = "@style/Theme.ExampleActivity" tools:targetApi = "31" > < activity android:name = ".MainActivity" android:exported = "true" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > </ application > </ manifest > |
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. The Declaration Code is given below.
<activity android:name=".SecondActivity" android:exported="true" > </activity>
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 the 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 Application
For any service that the Developer wants to use in the Android App, like Internet service, Bluetooth service, Google Maps, App Notification service, etc, the Developer needs to take the permission of the Android System. All those Permissions or Requests must be declared within the Manifest File. Open the AndroidManifest.xml file, with the procedure shown above. Let’s say the user needs to add Internet permission, that needs to be accessed by the app. Add the below Internet Permission within the manifest tag.
<uses-permission android:name="android.permission.INTERNET" />
Let’s say one needs to add Bluetooth permission within the app, then it must be declared like this:
<uses-permission android:name="android.permission.BLUETOOTH" />
Note: Let’s say that the MainActivity extends AppCompatActivity in the code snippet given at the top, internally the structure is as shown in the image given below. It can be seen that AppCompatActivity also extends the Activity Class.
Contact Us