Codementor Events

Getting started with Android  Activity And Lifecycle

Published Apr 03, 2020
Getting started with Android  Activity And Lifecycle

An Activity in Android is one of the most significant Components in Android. It is where we put the UI of our application. In this way, on the off chance that you are new to Android advancement, at that point you ought to realize what an Activity is in Android and what is the lifecycle of an Activity. Right now, will find out more about Activity and its lifecycle. Along these lines, we should begin

What is an Activity in Android?

At whatever point you open an Android application, at that point you see some UI over your screen. That screen is called an Activity. It is the essential segment of Android and at whatever point you are opening an application, at that point you are opening some Activity.

For instance,

when you open Whatsap or Telegram application, at that point you see your messages on your screen. Those messages are available in an Activity. On the off chance that you open some specific email, at that point that Message will be opened in some other Activity.

Along these lines, an Activity is a class that gives a window to draw the UI of your application. The principal screen that is propelled when you open an application is for the most part called MainActivity. You can call any Activity from any Activity.

At the point when we as a whole began with coding, we think about the principle technique from where the program starts execution. Correspondingly, in Android, Activity is the one from where the Android Application begins its procedure. Action is one screen of the application's UI. There is a progression of strategies which runs in a movement.

There is a lifecycle related with each Activity and to make a blunder/error free Android application, you need to comprehend the lifecycle of Activity and compose the code in like manner.

Lifecycle callbacks

This area gives reasonable and usage data about the callback techniques utilized during the movement lifecycle.

A few activities, for example, calling setContentView(), have a place in the movement lifecycle techniques themselves. In any case, the code executing the activities of a reliant segment ought to be put in the part itself. To accomplish this, you should make the reliant part lifecycle-mindful.It Consists of different methods such as .

onCreate() :

It is considered when the Application is first made. At the point when a client opens the application then some Activity is made. You need to actualize this technique in each action in light of the fact that, inside this strategy, all the important parts of your movement will be introduced. Here the introduction of your application's UI is finished.

onStart():

This strategy is considered when an action gets obvious to the client. At the point when all the introduction is finished by the onCreate() technique, at that point this Method is called.

onResume():

It is called not long before the client begins collaborating with the application. The majority of the center functionalities of the application are executed right now.

onPause():

It is considered when the Application is stopped for example it is for the most part gotten back to when you press the or home catch of your Android gadget. It means that the client is leaving the App and beginning some other action.

onStop():

It is considered when the Application is never again noticeable to the client. On the off chance that you are beginning another action, or some current action is going into onResume() state, at that point the present movement won't be obvious to the client and is halted.

onRestart():

It is considered when the movement in the halted state is going to begin once more. Thusly, the condition of the Application from the time it was halted will be reestablished.

onDestroy():

It is  called when the action is completely crushed for example at the point when you clear the application stack then onDestroy() will be called and all the conditions of the movement will be obliterated.

In this way, these are the techniques that are related with the lifecycle of a movement. Presently, we should take a gander at one commonsense model, yet before that, you have to get familiar with the nuts and bolts of Intents in Android since we are going to utilize them in our model.

INTENTS

Purposes are the informing body utilizing which we can convey between all the Android segments. Plan additionally conveys the data to go between the segments for example we can utilize purposes to pass some message between certain exercises or in the middle of some other Android segments. Expectations are of two sorts:

Implicit :

The certain expectation is utilized to determine some errand and all the application that can do that assignment can play out the undertaking. For instance, while utilizing an application, you can set an understood plan that you need photographs and the skilled application will open and give you the required photographs simply like Whatsaap application.

Explicit:

When a user opens another activity from a particular activity within the same application, like opening settings screen in your app.

Example

we are finally understood the basics of Activity, Activity lifecycle and Intents. We should begin with building up our application in Kotlin. We will build up an application which comprises of three screens and on opening the application, we will see the principal screen with two catches tapping on which will open those other two screens. In this way, fundamentally, we have one MainActivity and from that action, we are propelling the other two exercises.

The following are steps of creating Android App In Android Studio

Open Android Studio

Create New Project. On the off chance that you previously had a venture open, at that point make another undertaking by heading off to the File alternative in the upper left menu and select New > New Project.

Include the application name, the application index, and other required subtleties and afterward select the "EmptyActivity" format. (Select the language as Kotlin)

As a matter of course, you will get a MainActivty.kt record in Java organizer and activity_main.xml in res > format envelope. These the two records for example the code document and the UI record together make one action. Include two catch in the format record and these two catches will be utilized to dispatch the other two exercises from the MainActivity. The code for the activity_main.xml will be:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity"
    app:layout_constraintBottom_toTopOf="@+id/btnGetmeanapp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnIntent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Intent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnActivity" />

</android.support.constraint.ConstraintLayout>

Here, we have two buttons "Activity" and "Intent".
Presently, we have to make the other two exercises. To make an Activity in your undertaking, go to File > New > Activity > EmptyActivity. Enter the name of the action as Activity1.

Thus, make another action and name it as IntentActivity. For both the exercises, two documents will be made for example the Kotlin record and the format document. Since you are making a movement, so there is no compelling reason to interface these documents in light of the fact that, these records are consequently associated.

Inside the two recently made XML design records you can put one TextView with the goal that when you open them you can see various writings for various exercises. Following is the code for the activity_1.xml record:

activity_1.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity 1 hello"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Following is the code for the activity_Intent.xml file:

activity_Intent.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="What an Intent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Presently, the significant part. We need to associate both these Activities with the Buttons on the MainActivity page. When the user clicks the Button, different Activities will begun. Along these lines,we need some kind of listener that will be triggered when the user will press a button. The listener That is responsible for this is called setOnClickListener(). This technique will be considered when a Button is clicked and inside this strategy, we will compose the code for opening the action.

To open The Activity, we need to use some intent. In this way, to open the Activity1, we are utilizing the ActivityIntent and to open the Activity1, we are utilizing the Intent. Following is the code of our MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
         // opening Activity1
        btnActivity.setOnClickListener {
            var ActivityIntent = Intent(this, Activity1::class.java)
            startActivity(Intent)
        }

We have created an object from the Intent class and put two arguments in it this arguments are

this: It is used to tell from which context it will open the new activity, so specify this as a reference to the MainActivity because, from MainActivity, you are opening some other activity.
Activity1::class.java / Intent::class.java: It shows the activity that we need to open i.e. our target activity.

startActivity()

  • passing the purpose object right now dispatch the new action.

run your application in the cell phone or on Android Emulator. You can utilize the "Green Play" button present in Android Studio to run the application, or you can utilize Shift + F10 to run your application.

Press both the Buttons present on the MainActivity and you will see various exercises propelling when you press the Buttons.

Congratulations we are now ready to dive deeper and deeper in Android as we dive in the next article

for any issues please let me know

Discover and read more posts from Dancan Yego
get started