Search code examples
androidcrashandroid-lifecycleapplication-lifecycle

Activity seems to be created before application object


I have been receiving some crash reports in the Play Store which initially seemed crazy to me. Some activities (in 1 case, it's a broadcast receiver) are crashing in onCreate()/onResume() due to NullPointerException.

These activities are using static methods which in turn use the Application singleton [for context], but the object returned is null like if the application object didn't exist at all. From my understanding, the application should always have an instance.

My code is ordinary, the application sets a static field in its onCreate(), and the classes that the activities call use MyApplication.getInstance() which returns the application object.

How can MyApplication.getInstance() return null when called from activities? I have no idea how that could happen.

Of course, I have been unable to replicate this crash. This mainly happens in Android 6, but I have some reports from Android 8 and 9.


Solution

  • I think I found the cause. In Android 6 auto restore not initializing app, the problem is the same and they identified the cause as being the auto backup feature that was introduced in Android 6. Basically, after a restore from the backup, the app is restarted in a weird way where the Application object is not created before the Activity. We now can reproduce the problem and disabling backup fixes the problem.