Skip to main content

Fix Android Launch From Home

Launching a Capacitor app on an Android device typically initiates the display of your application's content in a webview. However, if the user is in the process of authentication and leaves the app it might not display the login page in the same state when the user returns. To fix this behavior you would want the app to direct users back to the login page instead of the application's content in such scenarios. To achieve this, specific modifications are necessary, as outlined in the following steps.

Go to your Capacitor Android project and navigate to the directory that contains your application's package. Your path will be something like android/app/src/main/java/io/ionic/starter/. However, the end of the path will reflect your unique bundle ID.

Create a new Java file in this folder named LauncherActivity.java.

LauncherActivity.java

_16
package io.ionic.starter;
_16
_16
import android.content.Intent;
_16
import android.os.Bundle;
_16
import androidx.appcompat.app.AppCompatActivity;
_16
_16
public class LauncherActivity extends AppCompatActivity {
_16
@Override
_16
protected void onCreate(Bundle savedInstanceState) {
_16
super.onCreate(savedInstanceState);
_16
Intent i = new Intent(this, MainActivity.class);
_16
i.replaceExtras(this.getIntent());
_16
startActivity(i);
_16
finish();
_16
}
_16
}

Copy and paste the provided code into the new activity file (LauncherActivity.java).

LauncherActivity.java

_16
package your.project.identifier;
_16
_16
import android.content.Intent;
_16
import android.os.Bundle;
_16
import androidx.appcompat.app.AppCompatActivity;
_16
_16
public class LauncherActivity extends AppCompatActivity {
_16
@Override
_16
protected void onCreate(Bundle savedInstanceState) {
_16
super.onCreate(savedInstanceState);
_16
Intent i = new Intent(this, MainActivity.class);
_16
i.replaceExtras(this.getIntent());
_16
startActivity(i);
_16
finish();
_16
}
_16
}

Replace io.ionic.starter with your project's identifier.

LauncherActivity.java
AndroidManifest.xml

_12
<activity
_12
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
_12
android:name="io.ionic.starter.MainActivity"
_12
android:label="@string/title_activity_main"
_12
android:theme="@style/AppTheme.NoActionBarLaunch"
_12
android:launchMode="singleTask"
_12
android:exported="true">
_12
<intent-filter>
_12
<action android:name="android.intent.action.MAIN" />
_12
<category android:name="android.intent.category.LAUNCHER" />
_12
</intent-filter>
_12
</activity>

Navigate to the AndroidManifest.xml file which is located at the following file path android/app/src/main/ and locate the activity section.

LauncherActivity.java
AndroidManifest.xml

_21
<activity android:name="io.ionic.starter.LauncherActivity"
_21
android:label="@string/title_activity_main"
_21
android:theme="@style/AppTheme.NoActionBarLaunch"
_21
android:exported="true">
_21
<intent-filter>
_21
<action android:name="android.intent.action.MAIN" />
_21
<category android:name="android.intent.category.LAUNCHER" />
_21
</intent-filter>
_21
</activity>
_21
<activity
_21
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
_21
android:name="io.ionic.starter.MainActivity"
_21
android:label="@string/title_activity_main"
_21
android:theme="@style/AppTheme.NoActionBarLaunch"
_21
android:launchMode="singleTask"
_21
android:exported="true">
_21
<intent-filter>
_21
<action android:name="android.intent.action.MAIN" />
_21
<category android:name="android.intent.category.LAUNCHER" />
_21
</intent-filter>
_21
</activity>

Update the AndroidManifest.xml to set the new Activity as the launcher activity. Add an intent-filter to LauncherActivity to define it as the launcher.

LauncherActivity.java
AndroidManifest.xml

_17
<activity android:name="io.ionic.starter.LauncherActivity"
_17
android:label="@string/title_activity_main"
_17
android:theme="@style/AppTheme.NoActionBarLaunch"
_17
android:exported="true">
_17
<intent-filter>
_17
<action android:name="android.intent.action.MAIN" />
_17
<category android:name="android.intent.category.LAUNCHER" />
_17
</intent-filter>
_17
</activity>
_17
<activity
_17
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
_17
android:name="io.ionic.starter.MainActivity"
_17
android:label="@string/title_activity_main"
_17
android:theme="@style/AppTheme.NoActionBarLaunch"
_17
android:launchMode="singleTask"
_17
android:exported="true">
_17
</activity>

Remove the intent-filter on the MainActivity that previously defined it as the launcher.

LauncherActivity.java
AndroidManifest.xml

_17
<activity android:name="io.ionic.starter.LauncherActivity"
_17
android:label="@string/title_activity_main"
_17
android:theme="@style/AppTheme.NoActionBarLaunch"
_17
android:exported="true">
_17
<intent-filter>
_17
<action android:name="android.intent.action.MAIN" />
_17
<category android:name="android.intent.category.LAUNCHER" />
_17
</intent-filter>
_17
</activity>
_17
<activity
_17
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
_17
android:name="io.ionic.starter.MainActivity"
_17
android:label="@string/title_activity_main"
_17
android:theme="@style/AppTheme.NoActionBarLaunch"
_17
android:launchMode="singleTask"
_17
android:exported="false">
_17
</activity>

On the MainActivity change exported to false and make sure to keep launchMode as singleTask.

Copy and paste the provided code into the new activity file (LauncherActivity.java).

Replace io.ionic.starter with your project's identifier.

Navigate to the AndroidManifest.xml file which is located at the following file path android/app/src/main/ and locate the activity section.

Update the AndroidManifest.xml to set the new Activity as the launcher activity. Add an intent-filter to LauncherActivity to define it as the launcher.

Remove the intent-filter on the MainActivity that previously defined it as the launcher.

On the MainActivity change exported to false and make sure to keep launchMode as singleTask.

LauncherActivity.java

_16
package io.ionic.starter;
_16
_16
import android.content.Intent;
_16
import android.os.Bundle;
_16
import androidx.appcompat.app.AppCompatActivity;
_16
_16
public class LauncherActivity extends AppCompatActivity {
_16
@Override
_16
protected void onCreate(Bundle savedInstanceState) {
_16
super.onCreate(savedInstanceState);
_16
Intent i = new Intent(this, MainActivity.class);
_16
i.replaceExtras(this.getIntent());
_16
startActivity(i);
_16
finish();
_16
}
_16
}

Be sure to run npx cap sync after applying these changes.

To test that you have correctly applied these changes:

  1. Launch your application with an Android device.
  2. Go to the login page.
  3. Return to the home screen of your Android device.
  4. Launch your app again by pressing the icon.
  5. Your application should now open displaying the login page in the state it was left in.