Android. How to change the default activity

April 25, 2017 30 Yehor Rykhnov

When you create a new project in Android Studio, is also created a default activity, MainActivity, the class MainActivity.java, and the activity_main.xml. About how to change the default activity is described below.

You can configure the initial activity of your application using the parameter intent-filter in file AndroidManifest.xml (app/manifests/AndroidManifest.xml).

By default, the AndroidManifest.xml file will look something like this:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Add a new activity, for example, right-click "app" => "New Activity" => "Empty Activity". In file AndroidManifest.xml (app/manifests/AndroidManifest.xml) added a new activity:

<activity android:name=".MySuperActivity"></activity>

All code AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.devreadwrite.btnanim.animationbutton">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MySuperActivity"></activity>
    </application>

</manifest>

To change the start activity, you need to delete the lines intent-filter (14 - 17) from the old activity and add them to the new activity, for example MySuperActivity (The MySuperActivity class and the view for it must be created to avoid errors). Thus we obtain the following:

<activity
    android:name=".MySuperActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Or interchange places the names of the activity.

We can also add the label attribute for the activity (the content of which will be displayed in the title of the view), an example:

<activity
    android:name=".MySuperActivity"
    android:label="@string/my_super_name">>
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".MySuperActivity" android:label="@string/my_super_name">

or

<activity
    android:name=".MySuperActivity"
    android:label="Hello world!">>
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

The final view of the file AndroidManifest.xml (app/manifests/AndroidManifest.xml):

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity">
    </activity>

    <activity android:name=".MySuperActivity"
        android:label="Привет мир!">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

In this way, you can change the starting activity (by default) from MainActivity to MySuperActivity.

From all the above described it is worth to remember the following, to change the default activity to its own it is necessary to delete (or better cut Ctrl + X) following code:

<intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

From all the above described it is worth to remember the following, to change the default activity to its own it is necessary to delete (or better cut Ctrl + X) following code <activity></activity>.