How to set the main activity in an Android Project

When you first create an Android project in Eclipse, it will set that activity to be the main or launching program. Let's say now you create another activity and realize that you want that one to be called as the main or launching activity.

Then you must edit the AndroidManifest.xml file. When an Android application is asked to run, the host loads the application and reads the AndroidManifest.xml file. It looks for an activity or activities with an intent-filter that has the MAIN action with a category of LAUNCHER and starts that activity. Take the following code out of your first activity and put that in your new activity.

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

Sample AndroidManifest.xml file

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


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Activity1"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name="Activity2"  android:label="@string/app_name">
                <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.