Search code examples
androidsecurityterminate

How to force killing application when minimizing in Android


Right now I'm developing some kind of password manager & generator application and it is almost done but I'm facing a problem that seems a bit hard to solve.

In order yo improve the security of my app, I added a timer so if the user is under inactivity the app will close. Now I'd like to close the app every time the user want to minimize it (pressing the "HOME" button and the "RECENT APPS" button).

I've tried onStop() and onPause() but nothing works because every activity on my app when is replaced by another moves through the mentioned states. Since I can't use Keycode == Keycode.HOME how could I do that?


Solution

  • I suggest that all your activities keep track of their state in onResume() and onPause(): they can increment some static application-wide variable counting how many of your activities are visible. When this count goes to zero, it means none of your activities are visible and you can do your cleanup.

    The solution proposed by @Don Chakkappan is not good enough I think, as your activity can be reported as active even though it's no longer visible.