I have an app that acts as a Launcher. This app has 3 activities:
SplashActivity
: shows a splash screen while loading, then launches LauncherActivity
and finishes. This is the Activity marked as launcher in the manifest.
startActivity(Intent(this, LauncherActivity::class.java))
finish()
<activity
android:name=".SplashActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
LauncherActivity
: main activity for Launcher. Has a menu button that launches DashboardActivity
.
startActivity(Intent(this@LauncherActivity, DashboardActivity::class.java))
<activity
android:name=".LauncherActivity"
android:launchMode="singleTask"
android:screenOrientation="landscape" />
DashboardActivity
: shows a list of apps and launches them through their launch intent.
private val DEFAULT_FLAGS_APP_LAUNCH = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(packageManager.getLaunchIntentForPackage(packageInfo.packageName).apply {
flags = DEFAULT_FLAGS_APP_LAUNCH
})
<activity
android:label="@string/apps"
android:theme="@style/TNA"
android:name=".DashboardActivity"
android:launchMode="singleTask"
android:screenOrientation="landscape" />
All activities are launched through startActivity
, including the apps.
I want the standard Android Launcher behavior, that is: when entering an app through DashboardActivity
, if I click home button, go to the main Launcher activity (LauncherActivity
), and when clicking back, go to the dashboard (DashboardActivity
).
The problem I have is that when clicking home, it goes back to DashboardActivity
, not to LauncherActivity
. If I finish DashboardActivity
, then when clicking back on an app, it goes back to LauncherActivity
.
Any ideas on how to solve this?
This is definitely back/task stack related. See this link for more information about the task stack.
When you go from LauncherActivity
to DashboardActivity
, the dashboard is placed on to the task stack. When the LauncherActivity
is requested again via the HOME button, the task stack is restored back to the last Activity
which was in use after launching the LauncherActivity
, which was DashboardActivity
.
You have several different options to resolve this:
Activity
for the "dashboard". Consider a drawer or even a Fragment
which shows the content and can be popped back to the main LauncherActivity
when it is done calling startActivity
to launch another app.DashboardActivity
calls startActivity
, it should call finish()
so it will get popped off the current task stack.singleInstance
mode, preventing multiple instances of the launcher Activity
to run at the same time. Note that you'll need to support onNewIntent
in your LauncherActivity
.FLAG_ACTIVITY_NO_HISTORY
when launcher your DashboardActivity
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With