Android: Find bugs with the “don’t keep activities” developer setting

Fixing bugs in your app can be a difficult job. Especially if you can’t reproduce a crash that happens in your live app or on your clients phone. Luckily Android will give you some developer settings that you can enable to make your phone run in a “non normal” way. Here we will have a look at the “Don’t keep activities” setting and show why it is useful to test your app from time to time with this setting switched on.

Where to find it?

As a normal app developer you should have enabled the “developer options” on your phone anyways. If not: go into your “settings – about phone/device – software information” and click seven times (or more) on the “Build number”. After that you can access the developer menu and can enable the “Don’t keep activities” setting:

What does it do?

The Android system will destroy an activity as soon as it sopped. With this setting you can simulate the behavior that the system kills a background activity due to memory issues. That could happen sometimes when your live app is in use for a while but it might not happen in your development testing. This feature will just kill every activity that you close right away. In a normal lifecycle many items will be cached to reduce loading times and you will see that closing and opening windows is a bit slower when the setting is switched on.

When to use it?

First: you should only use it during development and switch it off when you are done! It will slow down your system and can lead to unexpected errors in other apps!

If you experience random crashes in your app that you can’t reproduce it could help you to find those bugs. For example I was able to find a bug in a external module that cleared a variable in the “onDestroy” method but didn’t initialize it back again correctly. During a normal test run the error never occurred and everything is working fine but when the OS decides to destroy that activity it would have caused a crash or make the whole API calls unusable.

Other useful settings

In my previous tutorial “Optimize Android app performance” you can see some other very useful settings to debug and optimize some UI parts of your app. There are many other special options in there to simulate different behaviors of your app that aren’t “normal” but help to find edge cases and reduce bugs. Always browse around in the developer settings especially if you use bluetooth devices or rely on default system settings.

Like the content?

If you like the tutorials, plug-ins or want to see more please consider becoming a Github sponsor.