Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Manifest- intent filter and activity

Could someone explain the following lines in the manifest -

    <activity
        android:name=".AboutUs"
        android:label="@string/app_name">
        <intent-filter >
            <action android:name="com.example.app1.ABOUT" />
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>

    </activity>    

How are the fields in activity and intent filter important and when are they used/referred ? Sorry, i tried to read the documentation but still couldnt figure much out.

Thank you

like image 683
Ankit Rustagi Avatar asked May 18 '13 13:05

Ankit Rustagi


3 Answers

android:name=".AboutUs"

This is the name of your Activity class, the dot at the front is shorthand notation for your package. So this actually stands for com.your.package.name.AboutUs which means your java file that represents this Activity is called AboutUs.java

android:label="@string/app_name"

label is the string that gets shown in the launcher(if the activity is listed in the launcher) and at the top of the window when the activity is open.

<intent-filter > ... </intent-filter>

intent filter defines the Intents that your activity "listens for" in order to launch.

<action android:name="com.example.app1.ABOUT" />
<category android:name="android.intent.category.DEFAULT"/>

Action and category are both fields that get set on an Intent before it is "fired off" into the system. The system will then look for any activities that match both the action and category and if it finds one then it will launch that activity, or if it finds multiple it will show the user all of them and let them pick.

In your case your the action you are listening for com.example.app1.ABOUT is a custom action that is specific to your app, not one of the systems actions.

So here is what an intent that would start this particular activity might look like:

Intent i = new Intent();
i.setAction("com.example.app1.ABOUT");
i.addCategory("android.intent.category.DEFAULT");
startActivity(i);

Note that because you've created a custom action, this intent does not require access to your AboutUs.class so this intent could technically be fired from any app on the device and it would launch into your activity.

like image 104
FoamyGuy Avatar answered Oct 11 '22 12:10

FoamyGuy


For the ACTION you have this answer and for the DEFAULT category you have this answer

like image 1
4 revs, 2 users 76% Avatar answered Oct 11 '22 10:10

4 revs, 2 users 76%


An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. To declare your activity, open your manifest file and add an element as a child of the element like your example.

  • The android:name attribute is the only required attribute—it specifies the class name of the activity.
  • The android:label attribute is a user-readable label for the application as a whole, and a default label for each of the application's components

An element can also specify various intent filters—using the element—in order to declare how other application components may activate it.

  • The element specifies that this is the "main" entry point to the application.
  • The element specifies that this activity should be listed in the system's application launcher (to allow users to launch this activity).

Please refer to the http://developer.android.com/guide/components/activities.html

like image 1
rookiejava Avatar answered Oct 11 '22 10:10

rookiejava