An activity is one screen of an app. In that way the activity is very similar to a window in the Windows operating system.
The most specific block of the user interface is the activity. An Android app contains activities, meaning one or more screens.
Examples: Login screen, sign up screen, and home screen.
An activity class is per definition a class in Android. Every application which has UI must inherit it to create a window.
An activity in Android is a specific combination of XML files and JAVA files. It is basically a container that contains the design as well as coding stuff.
XML files provide the design of the screen and JAVA files deal with all coding stuff like handles, what is happening, design files, etc. JAVA files and XML files make your activity complete.
Java file:
XML file:
To make an app we need activities that are bound loosely with each other. In the Android manifest we can organize the activities in parent-child relationships to aid navigation. The “main activity” is called the first activity.
Example: In social media, e-commerce, and other apps, you see the main activity whenever you open up that app.
Activities must be declared and their certain attributes must be declared in the manifest so that we can use the activities in our app. Android manifest is a file that will give you information about the application.
Let’s start by declaring our activity. First, open your manifest file then add an
Example:
1 2 3 4 5 6 7 8
<manifest ... > <application ... > <activity android:name=".ExampleActivity" /> ... </application ... > ... </manifest > seDebugValue(value)
This feature is very powerful. The intent filters tell Android which activities can handle which actions. If Android is given an intent, it should find out what activities can manage it. This process is known as intent resolution. Intent filters provide explicit intent and implicit intent. You can use intent filters by declaring
Example:
1 2 3 4 5 6 7
<activity android:name=".ExampleActivity" android:icon="@drawable/app_icon"> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter> </activity>
In this example,
<action> element :- this activity sends data.
<category> element :- if we declare it as DEFAULT then it will allow the activity to get the launch requests.
<data> element :- represents the type of data that an activity can send.
Example:- how to call the activity described.
1 2 3 4 5 6 7
// Create the text message with a string Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.setType("text/plain"); sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage); // Start the activity startActivity(sendIntent);
If you don’t want to use any activity in other applications then you should not use the intent filter, and you can start them by using explicit intents.
To control whether apps can start a specific activity we can use manifest’s <activity>. If parent activity and child activity both have the same permissions it will not launch.
Example:- Let’s say you have a socialApp that shares a post on social media, then your social media app should define permissions that an app
calling it must have.
1 2 3 4 5
<manifest> <activity android:name="...." android:permission=”com.google.socialapp.permission. SHARE_POST” />
Your app must match the permission set in socialApp’s manifest so it can be allowed to call socialApp.
1 2 3 4
<manifest> <uses-permission android:name="com.google.socialapp.permission. SHARE_POST" /> </manifest>
An activity goes through a number of states. Activity lifecycle in Android manages the different states of activity, such as when the activity starts or stops, etc. All of these states are managed by callback methods.
There are seven callback methods:-
onCreate():- This callback method must be created by you, this is fired when your activity is created by the system. For example, when you open the app it will call onCreate(), onStart(), and onResume() methods. These three methods are initiated when you open an app.
onStart():- There should always be an onStart() callback after onCreate() finishes. In the started state users will be able to see the activity but they are not ready for user interaction.
onResume():- In this, activities will be in the foreground and ready for user interaction. You can understand the use of this callback from this example: “when the user presses the phone’s answer button it will give onPause() after you end up calling it will again give onResume()”.
onPause():- This callback occurs when there is a case of another activity starting in front of the current activity. It is the opposite of onResume(). An example for this callback can be “when you open the app, that is another app from the notification bar, or open settings then it will call onPause() and onStop()”.
onStop():- It is the opposite of onStart(). In this case, activity will no longer be visible to the users.
onRestart():- This occurs when the activity is stopped and before the activity starts again. It is a less common callback.
onDestroy():- It is opposite of onCreate(). This starts when the system needs to free memory or when finish() is called. It is used for cleanup which is a very common activity.