0

I want to detect when my app minimizes by the hardware minimize button or code and launch itself again when minimized, i'm using the following code but it does nothing. I think it launches itself but in background.

@Override
    protected void onUserLeaveHint() 
   { 
     launchApp();
        System.out.println("Home Button Pressed");
        super.onUserLeaveHint();
    }

i am calling my intent from the launchApp() method

MaggotSauceYumYum
  • 402
  • 1
  • 5
  • 23
  • You can't force the device to run your app in front. However, you can override onPause nor onStop to perform background stuff before it's minimized/closed. – Neria Nachum Nov 15 '15 at 15:41

1 Answers1

0

You can use Application.registerActivityLifecycleCallbacks() (API >= 14) as described in this answer to execute code if your app goes in the background and is not resumed within a time limit.

However, for most apps you should not need to do this and there might be a better way to achieve what you need - can you explain your use case?

For example, if you need to process long running tasks even when the app is not visible, you could use a Service. Or if you are building an app that is meant to be always visible for some demo purposes, you could set it as a custom launcher.

Community
  • 1
  • 1
Matej Snoha
  • 497
  • 6
  • 10