Search code examples
androidhomescreenandroid-homebutton

Home Button that doesn't work only while my own app is in foreground


I have created a Home Screen replacement app. As per some of the suggestions here on SO, I approached this by creating the following two activities:

<activity
        android:name=".Activities.GhostLauncherActivity"
        android:enabled="false"
        android:label="@string/title_activity_ghost_launcher">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>

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

<activity
        android:name=".Activities.HomeScreenActivity"
        android:enabled="true"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>

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

Then toggling their enabled state as follows:

ComponentName component = new ComponentName(this, GhostLauncherActivity.class);
ComponentName secComponent = new ComponentName(this, HomeScreenActivity.class);

packageManager.setComponentEnabledSetting(component, packageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
packageManager.setComponentEnabledSetting(secComponent, packageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

Intent intent = new Intent(Intent.ACTION_MAIN);

intent.addCategory(Intent.CATEGORY_HOME);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

packageManager.setComponentEnabledSetting(component, packageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

This works like a charm. Every time I run the Java code above, the user is prompted to select a Home Screen app, with mine listed as one of the options. The HomeScreenActivity is itself a launcher screen: there is some GridView of apps that can be launched.

As per my question: when an app is launched from the Home Screen, pressing Home will return you to HomeScreenActivity. Perfect.

However, you can also access the SettingsActivity from within the Home Screen. Simple enough, the following code exists somewhere:

Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);

Now when I press the Home Button, the first time everything works fine...but the second time I go to settings, the Home button does not work. My app remains on the Settings screen. Even setting aside the the fact that it worked the first time, what could cause the Home button to stop working in my Home Screen replacement app? I have selectively commented out chunks of code from both Home Screen and Settings, exhaustively to check if any particular code chunk was responsible: none seem to be the culprit.

This error seems to be happening at some higher level and I can not figure out what that is. I am running Android 5.0 on my device. Has anyone experienced anything similar and if so, how can I address this issue or where else should I search for fixes within the code?


Solution

  • I was having the exact same problem; trying to launch my own activity from my home screen app. I found that adding the following line to my HOME activity and the sub-activity in the manifest fixed it:

    android:launchMode="singleInstance"