Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launcher development - Home button not going back to initial activity

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?

like image 466
m0skit0 Avatar asked Jan 31 '18 16:01

m0skit0


1 Answers

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:

  1. Don't use a separate 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.
  2. After your DashboardActivity calls startActivity, it should call finish() so it will get popped off the current task stack.
  3. Usually launchers are setup to be launched in 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.
  4. To prevent odd interactions with the task manager, consider setting FLAG_ACTIVITY_NO_HISTORY when launcher your DashboardActivity.
like image 157
Larry Schiefer Avatar answered Oct 06 '22 14:10

Larry Schiefer