What android.intent.action.MAIN explained

When developing an Android application, you will see an android.intent.action.MAIN in your AndroidManifest.xml file.

An example of the manifest file could be as follows:

        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />

The android.intent.action.MAIN is defined to let the Android Operating System (OS) knows what Activity class to run when the application is started.

In the above example, the <intent-filter> tag is added to the <activity> tag with the name of .MainActivity.

By setting the action.MAIN intent filter, the Android OS will run the MainActivity class when you open the application.

When you generate an Android application using Android Studio, the action intent filter is usually paired with the category intent filter of category.LAUNCHER.

The category filter is used to let Android OS know what activity to run when the application is started from the launcher.

In Android terminology, a launcher is a specific type of Android application that can be used to start other applications.

In Android phones and tablets, the launcher is the home screen of the device.

Both the action and category intent filters are required to determine the Activity class to run when the application is launched from an Android device.

Without them, the application won’t be launched and Android Studio will respond with the following error:

Could not identify launch activity: Default Activity not found
Error while Launching activity
Failed to launch an application on all devices

Since android.intent.action.MAIN only let Android OS know the main activity to run, you can pair it with other category filters.

For example, you can pair an action.MAIN filter with the category.DESK_DOCK filter to define the Activity to run when the device is inserted into a desk dock.

To conclude, a standard Android phone application requires an activity with both action.MAIN and category.LAUNCHER filters.

The filters let the Android OS know what Activity class to look for when the application is launched.

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.