While the application is running, it sometimes happens that other applications open, thereby taking away the focus from my application.

Question: how in such situations to restart the application in the services, where to dig?

  • one
    At this moment onPause () is called - Sergey Gornostaev
  • Then onStop() , because onPause() will work for the activit when, for example, it is blocked by Dialog - tim_taller

1 answer 1

You need to implement a Service in which to start some kind of timer that will check the ActivityManager and see which is currently in the topActivity stack. And if it's not yours, then start the right one.

UPD.

 void check() { // получение Activity из верхнего таска ComponentName componentName = (mActivityManager.getRunningTasks(1).get(0)).topActivity; String s = componentName.getPackageName(); // сравнение текущего названия package с тем, который запущен if (!getPackageName().equals(s)) { // запуск своего приложения } } 
  • Can you tell me more about Coking?) - DevOma
  • @TITAN, see UPD. draft method - tim_taller