5

I am working on my own home launcher replacement and it works fine but one thing bothers me. When I press a home key, current home activity (the one defined in manifest as main/defualt/launcher/home) restarts - current activity instance onpause is executed and oncreate is fired again, so new activity is brought up.

On the other hand, ADW launcher and LauncherPRo does not behave like that - I do not a refresh like in my case. Launcher Pro even can do several actions:

  1. If you are on the main screen with app icons, it zooms out to see a snapshot of all screens,
  2. If you open a drawer and press Home, it just go back to the main screen.

Any ideas how to do that?

I just did a very simple prototype from scratch with just one activity (defined in manifest as main/defualt/launcher/home) and I see the same thing - it gets recreated if I press Home.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
tiptop
  • 51
  • 1
  • 2

2 Answers2

6

Add

if (!isTaskRoot()) {
    finish();
    return;
}

to the onCreate() of your first Activity (see Android application restarts when opened by clicking the application icon).

Community
  • 1
  • 1
JTatie
  • 93
  • 2
  • 5
  • `isTaskRoot` is true in both the activity's instance. When I use the above snippet, the screen is blank. Older activity instance is not destroyed as well. – Varsha Ravikumar Oct 14 '19 at 18:23
5

Add android:launchMode="singleInstance" to your <activity> element in the manifest.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • android:launchMode="singleTop" should also work fine. it would not be possible to call other activities from his one if singleInstance is used as I understand. – mishkin Dec 28 '10 at 22:07
  • 2
    @mishkin: I am going by the Home sample application from the SDK, which uses `singleInstance`. Curiously, the AOSP launchers use `singleTask`. – CommonsWare Dec 28 '10 at 22:10
  • adding related @CommonsWare answer for info on the "extra" questions posed: [Android launcher press home in launcher to go to default screen](http://stackoverflow.com/a/13203826/383414) – Richard Le Mesurier Sep 04 '13 at 12:52