0

Currently I am handling onTrimMethod(int level) in my app with ComponentCallbacks2.TRIM_MEMORY_COMPLETE and ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL. I get this callback easily with low end devices when my app is running in background and simultaneously I am playing games.

In that I put Runtime.getRuntime().exit(0); to kill the app in background only so that when user taps on app (running in background), app will restart from launcher activity. I don't know it kills the app or not but when I open the app, first it shows dialog with "Unfortunately, app has stopped" and app does not restarts.

If I start the activity from this method, it will launch the application and bring it to foreground without any user interaction.

I tried some other ways too to kill the app. They are System.exit(0); and android.os.Process.killProcess(android.os.Process.myPid()); but none of them are solving my problem.

So, what to write in onTrimMethod() or how to handle in other way so that when user opens the application (brings to foreground from background) it will start from the first activity?

pratik03
  • 651
  • 2
  • 7
  • 23
  • Do you really want to do that? OnTrimMemory is more for releasing memory from caches and what not. It isn't a signal that the app should be restarted. – Elletlar Jul 18 '18 at 14:06
  • Yes I want to restart the app. I have cleared the cache but that does not stop app to be crashed when bringing it to foreground. So my app is like once user signs in, he/she can use it, and I keep session and other relavant info in memory. So if I clear the cache, I need user to start from login screen. I want to know how to do that. – pratik03 Jul 19 '18 at 03:33
  • I don't think it is a good solution. Of course OOM problems can be very difficult, but the solution is to get rid of any warnings under 'Analyse -> Inspect Code' and to use 'Run -> Profile' to find the problem. But this thread has some hacks for 'restarting' the app : [How do I programmatically restart an Android app?](https://stackoverflow.com/questions/6609414/how-do-i-programmatically-restart-an-android-app) – Elletlar Jul 19 '18 at 09:11

0 Answers0