Android Activity

What is an Activity?

Android Activity is a screen in the user interface of an Android application. In this way, Android activity is very similar to the desktop application window. An Android application can unlimitedactivities,which means there is no restriction on the number of screens. An app launches by displaying the launcher activity, and then the application can allow an opening for further activities.

Activity Lifecycle

When the Android app is launched for the first time, basic activity is generated. The activity then undergoes three states, Create, start, and resume, which were generated before they were ready to serve the user.

Android Activity

Android Activity Lifecycle

When the current activity opens another activity (screen), these activities are undergoing the same three states when opened.

For example, if activity A opens another activity B, activity A gets paused and gets pushed into the stack. This means that activity A will be suspended. When the user clicks back to return to A, activity A returns to the resume state. When the user returns to the home screen of the Android device, all activity is paused and then stopped. After that, when the user returns to the app, the activity goes through the start state and resume state. If your Android device requires memory that Android applications occupy the device's memory, your app can be destroyed. In other words, the Android pushes the activity in a destroyed state.

The activity state always follows the arrows in the figure above. You cannot move an activity directly from creation to resume. Activities are always created, started, and restarted in that order. Resuming an activity can vary from suspend and resume from suspend to suspend, starting from the back, but cannot directly stop and resume.

An activity class represents all the activities in an Android application. These activity classes are subclasses of android.app.Activity. The Activity class contains a set of methods that correspond to the life cycle states that an activity can be in. The methods are as follows:

  • onCreate():

This is the first method, which is called on the creation of activity for the first time.

  • onStart():

When the application launches and theusercan see activity, this method is called.

  • onRestart():

When the activity is restarted after it has been stopped, onRestart() method is called.

  • onResume():

When the user makes interaction with the app, this method is called.

  • onPause():

When the current activity is minimized or a new activity is started, and the previous getspaused, onPause() method is called.

  • onStop():

When the activity is minimized or no longer visible, onStop() is called.

  • onDestroy():

When the android OS destroys the activity, onDestoyed()method is called.

Create Activity

The Activity class inherits from the "AppCompatActivity" base class. The name "Compat" indicates Compatibility, which means that even if the class is the latest version, it will support the previous features. The activity class does not have a self-generated init () block, so you cannot instantiate the activity class yourself. This is handled by the system itself and the programmer can change the content of the activity. The instance Activity class can be created using the following steps with the help of callbacks.

Follow these steps to create an activity:

1. First, right-click on Apps. Then select New -> Activity and select the activity according to your requirements.

Android Activity

2. Then define the activity in Configure Activity window in AndroidStudio. Enter "Activity Name" and "Package Name" in the text boxes and check the checkboxes to create the layout of the activity and click Finish.

Android Activity

A new activity with the name you entered will be created with the following code:

public class MainActivity2extends AppCompatActivity{


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//OnCreate() method
setContentView(R.layout.activity_main2);
    }


}

The function onCreate() is called when the Activity is created. setContentView() is used to set the layout that we declared in the XML file to the Activity.

R is the resource class in Android which is used to access the resources within the code segment.

An Android application can have an unlimited number of activities. Every activity you create for the application must be declared in the XML manifest file, and the launcher activity of the application must be declared with an intent filter, which comprises the action, and the category attributes, as shown below:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />


<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>


</manifest>

Related Topics

Android TextView

Widgets are user interface (UI) elements that help users to interact with Android applications. It is one of many equipments that can be used to improvise the user interface of...

4 minutes read.

What is Android Studio

Android Studio powered by the IntelliJ platform is an integrated development environment (IDE) and is officially recognized for developing androidapplications. Its foundation lies on JetBrains' IntelliJ IDEA and is specialized...

3 minutes read.

Content Providers of Android with Example

An essential part of Android that performs the function of a relational database by storing application data is called a content provider. The function of the content provider in the...

13 minutes read.

Android RecyclerView

RecyclerView is a ViewGroup brought to AndroidStudio as successor to ListView and GridView. These are all improvements and are included in the latest v7 support package. It was written to...

8 minutes read.

Android Styles and Themes

Android styles allow you to design the appearance (for example, colors, size, and fonts) of Android UI components from XML resource files. With this method, you only need to set...

5 minutes read.

How to insert the mobile number on an android application using EditText

Number Insertion in android application: Sometimes while dealing with numbers, we don't want to insert any string inside an number so, we premiliary define an EditText so we externally specify...

2 minutes read.

Broadcast Receiver in Android with Example

When a device wakes up, receives a message, receives an incoming call, switches to aeroplane mode, or initiates any other system-wide action, it is said to have been broadcast in...

5 minutes read.

Android ScrollView

A ScrollView is a view group that is used to create vertically scrollable views in an android application. Only a single direct child can be contained by ScrollView. For placing...

5 minutes read.

Event Handling in Android

Events are a convenient way to gather data about user interactions with interactive elements of your application. Like a button click or a screen touch. A queue of events on...

3 minutes read.

How to Generate QRCode in Android?

Many apps use QR codes to show data in a way that is machine-readable. Data is represented using these codes in a secure manner such that only computers, not humans,...

4 minutes read.

Button Widget in Android Tutorial

Explanation: Button is a widget under Buttons section which is used to execute specific code. Example: XML file: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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/button"         android:layout_width="wrap_content"    ...

1 minute read.

Assets folder in Android

You might have noticed that unlike the Eclipse ADT (App Development Tools), the Android Studio does not have an Assets folder, which is mainly used to store the web resources...

3 minutes read.

Jetpack Architecture Components in Android

A collection of software elements, libraries, tools, and instructions is called Android Jetpack. It is available to assist in creating reliable Android applications. Jetpack, which was introduced by Google in...

8 minutes read.

Android Tutorial

This Android tutorial will provide you with all the concepts that will be beneficial for both beginners and advanced-level students or candidates. You can learn this tutorial to begin your...

6 minutes read.

Android Broadcast Receivers

Android applications can send or receive broadcast messages from the Android operating system and other Androidapplications, like the publish-subscribe design pattern. When an event of interest happens,these broadcasts are sent. For...

4 minutes read.

Different Ways to Create aar File in Android Studio

A file called an Android archive file (*.aar) can be created using Android Studio and comprises classes and methods that use related files and classes for Android. The Android library...

4 minutes read.

How to insert an email in android application in android version 8

Email Address Insertion It is used to insert email adrress from the user in the android application.The only point of difference between an EditText and an Emailtext is that EmailText consisits...

1 minute read.

Android Navigation Drawer

A sliding menu used to show important links in the Android Application is called Navigation Drawer. The navigation drawermakes it easier to navigate between the fragments. It is not displayed...

5 minutes read.

Top Programming Languages for Android Development

For mobile apps, there are so many programming languages. You would need to spend days simply researching if you had to pick one from a list of 30 coding languages...

8 minutes read.

Android Emulator

A tool for creating Android virtual devices (including software and virtual hardware) on your personal desktop is known as Emulator. We can use these virtual devices as the target device...

4 minutes read.