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
.
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
.
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
.
Be sure to run npx cap sync
after applying these changes.
To test that you have correctly applied these changes:
- Launch your application with an Android device.
- Go to the login page.
- Return to the home screen of your Android device.
- Launch your app again by pressing the icon.
- Your application should now open displaying the login page in the state it was left in.