Search code examples
javaandroidandroid-activity

Application restart


I have been running into a problem with my application, which I have no idea why. The following is:

  • the application is a large, commercial project, which makes several connections to the database, with a login system and everything else
  • I noticed that if I leave the app in the background for about 10 minutes, for example, it terminates my connection and restarts everything again, forcing me to log in again.

The only guarantee I can give is that they are not connection problems with my server, as in my tests it never failed.

Anyone who might have any idea why?

(I know that the explanation of the problem was a little vague, but the situation is very vague even for me)


Solution

  • How did I find my problem?

    The user of my application was using a bluetooth barcode reader, which was programmed to automatically turn off after a period of inactivity. I noticed that whenever the bluetooth device turned off, my application would lose its previous state if it was in the background. Using the LogCat tool, I realized that my process was being killed by the system itself. The messages always looked like these:

    W/ActivityManager: Force finishing activity my.project/.view.activities.MyActivity

    I/ActivityManager: Process my.project (pid 12984) has died

    After much research, I found that the Android system interprets some external events as configuration changes, eg screen rotation change, Bluetooth device connection/disconnection, etc. When such a change happens, Android, by default, kills your app's process and restarts it completely again, so that the app adapts to the new behavior. In my case, there was a NullPointerException in the code, which I hadn't handled correctly, so the application went back to the beginning, losing its state data.

    However, in other application screens the mentioned Exception didn't occur (so it doesn't go back to login when starting), but even so I lost some screen data, like something that was typed in an EditText, for example.

    How did I solve it?

    On researching again, I found that you can let Android handle these configuration changes itself, telling it not to restart its process. To do this, just add in your Manifest, in the desired activity, the line:

    android: configChanges = "keyboard | keyboardHidden | navigation"
    

    As in my case the problem was with a bluetooth keyboard, I added these options keyboard | keyboardHidden; some keyboard models, for some reason, also change Android navigation, so I added navigation. After this change, done! No more problems!

    P.S. 1: Unfortunately, not everything always works out. Adding android: configChanges won't work if your activity has fragments (I'm still trying to figure out how to solve this).

    P.S. 2: This is not a good practice, I need to make that clear to you. For me, it's okay to do it this way, as my application responds well to changes. After all, my app is simple. Only use this feature that I explained if it is your last option or, if like me, your application is not so complex. Remember: this is not a magic solution to problems; in my specific case it worked fine, but for you, it might break your application.

    P.S. 3: I recommend taking a look at https://developer.android.com/guide/topics/manifest/activity-element#config, in the android subtopic: configChanges. Listed are all the configuration changes a device can make.