Friday, June 24, 2022

Activities in Android Studio

In the previous articles of our android studio tutorials for beginners we learned how to create an android studio project. By default android studio create packages and files from which is the Main Activity java file. Surely, an activity class is a crucial component of any android app. So, this article is important as we introduce activities in android studio. What is Activity class?, an activity represents the window in which the app draws it UI. The mobile app differs from desktop app in that a user’s interaction with the app doesn’t always begin in the same place. As in mobile app, you can launch an app from another one. Like, launching social media app from email app.

The Launcher Activity

Apps with multiple screen contains multiple activities. The first screen that appears to the user after launching the app called the launcher activity. Then, you can start any activity to perform specific tasks. In order to use activities in an app you must declare each activity in the manifest file. To declare an activity in manifest file add <activit></activity> as a child of application layer. The activity element must have one attribute (android:name=”.ExampleActivity”).

<manifest ... >
  <application ... >
      <activity android:name=".ExampleActivity" />
      ...
  </application ... >
  ...
</manifest >

Intent and Intent Filters

An Intent is a messaging object you can use to request an action from another app component. There are three main ways for intent to make communication between app components.

  • Starting Activity.
  • Starting Services.
  • Delivering Broadcasts.

Intent Types

There are two main types of Intents.

Explicit intents

Explicit intents

With this type of intent you specify the target app, package or class name. Moreover you can use this type to start a component in your own app, as you know the class name of the activity or service you want to start. For example, when you start known activity of service in your app.

Implicit Intents

Actually, in this type of Intent, you do not specify the target name, but instead declare a general action to perform, which allows a component from another app to handle it. For Example, when you want to send text from your with mail, you must specify the action for this intent (ACTION_SEND).

Declare Intent Filters

Intent filters are a powerful method to launch an activity based not only on an explicit request, but also an implicit one. For example, when you explicitly start Gmail App you tell the system to start Send Email Activity in this app. By contrast, when you use implicity intent you send request that tells the android system to start the activity that can do this task. Further more, you can use this feature by declaring an <intent-filter> attribute in the <activity> element.

<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>

From the above example, you tell the activity action to send dat. Also, category as Default which enables the activity to receive launch request. Finally, the data element used to specify the type of data that this activity can send. In order to call this activity see the following code.

Java Code

// 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);

Kotlin Code

val sendIntent = Intent().apply {
    action = Intent.ACTION_SEND
    type = "text/plain"
    putExtra(Intent.EXTRA_TEXT, textMessage)
}
startActivity(sendIntent)

If you want to not allow other apps to your app activities, you do not need to use intent filters, and you should start the using explicit intents.


Declare permissions

You can use the manifest’s <activity> tag to control which apps can start a particular activity. A parent activity cannot launch a child activity unless both activities have the same permissions in their manifest. If you declare a <uses-permission> element for a parent activity, each child activity must have a matching <uses-permission> element.

<manifest>
<activity android:name="...."
   android:permission=”com.google.socialapp.permission.SHARE_POST”

/>

Then, to be allowed to call SocialApp, your app must match the permission set in SocialApp’s manifest:

<manifest>
   <uses-permission android:name="com.google.socialapp.permission.SHARE_POST" />
</manifest>

Read more about activities from Google Developers Documentation

Thank You

RELATED ARTICLES

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments

KU NUR RASYIDAH BINTI KU BIDE on Custom Calendar View Android | Project Java Files
SHIVANT KUMAR PANDEY on Login And Registration by Firebase
Adriane Troy U. Alariao on Custom Calendar View With Events Android