Android Activity: is the main component of Android applications. The activity gives user interaction, you can set UI
setContentView(View) in activity to show the user interface. Like when you open mail apps (Gmail etc) you will see the list of emails. That is showing in a list on activity or using other components.
There are some methods have to override in Android activity:
onCreate(Bundle)for initializing activity and set the layout resource file and Using
findViewById(int)you have to interact with UI widgets like TextView, Button, CheckBox, RadioButton etc.
onPause()is where you deal with the user leaving your activity. Most importantly, any changes made by the user should at this point be committed.
Not only 2 methods there is many callback method , which handles life cycle of Activity: onStart() , onResume(), onStop(), onDestroy(), onRestart() etc.
Must Read About Multiple Activity :
Many apps have multiple screens, so every screen required a user interface, where a user can interact with the app. So, in this case, we have to use multiple activity or fragments can be also an option but its totally different concept you can read here: Android Fragment with an example in Kotlin
Typically, one activity in an app is specified as the main activity, which is the first screen to appear when the user launches the app. It should be defined in AndroidManifest.xml with <IntentFilter> “Action” and “category” to know android system your launching activity.
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Each activity can start another activity in order to perform different actions or show.
For example, the main activity has mails list (inbox), so on click any mail you have to show another user interface (Activity have UI). That can be performed by Intent to go form on activity to another with some data.
To use activities in your app, you must register information about them in the app’s android manifest xml, and you must manage activity lifecycles appropriately.
Configuring the manifest: Declare activities
To declare your activity, open your manifest file and add an <activity> element as a child of the <application> element. For example:
<manifest ... >
<application ... >
<activity android:name=".TestActivity" />
</application ... >
Enough theory, let’s start Practical :
This example demonstrates activity with Kotlin…
Step 1. Create new project “Build Your First Android App in Kotlin“
Step 2. Open project structure and see how activity relate in Android app
You can see the code of MainActivity have set view “activity_main.xml” and MainActivity has declared in AndroidManifest.xml.
Source codes : https://github.com/eyehunts/HelloWorld
MainActivity.kt : https://github.com/eyehunts/HelloWorld/../MainActivity.kt
activity_main.xml : https://github.com/eyehunts/HelloWorld/…/activity_main.xml
AndroidManifest.xml : https://github.com/eyehunts/HelloWorld/blob/…/AndroidManifest.xml
Note : This example (Project) is developed in Android Studio 3.1.3 . Tested on Android 9 ( Android-P), compile SDK version API 27: Android 8.0 (Oreo)
Coding in Kotlin