question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Initialization in Activity#onCreate() may cause a crash or race condition

See original GitHub issue

Initialization of Amplify in Activity#onCreate() may cause problems in the following scenarios though official documentation guides you to do so. Workaround is at the bottom of this issue.

  1. Start Activity and Amplify’s initialization happens
Activity#onCreate()
  Amplify.addPlugin(AWSApiPlugin())
  Amplify.configure(applicationContext)
  1. Hit back button
Activity#onDestroy() called
  1. Re-launch the App again
Activity#onCreate() called again
  Amplify.addPlugin(AWSApiPlugin())
  Amplify.configure(applicationContext) // this throws AmplifyException

The reason this happens is because Amplify is a singleton object and keeps its state with static boolean configured and will not be subject to GC immediately after Activity#onDestroy() is called. In this way, when Activity#onCreate() is called again, initialization is executed again, and a race condition occurs. The official documentation catches the AmplifyException immediately, but because of this exception the configure per CATEGORIES is not done in Amplify#configure(), the app is obviously not in a good state.

The easiest workaround is to initialize in Application#onCreate(). This is the most realistic way. No race condition occurs because the lifetime of the Amplify object is tied to the life cycle of the Application.

// your custom application
class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        try {
            Amplify.addPlugin(AWSApiPlugin())
            Amplify.configure(applicationContext)
        } catch (e: AmplifyException) {
            Log.e(TAG, e.message)
            throw RuntimeException(e)
        }
    }
}
// don't forget to add your app class in the manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="us.shiroyama.amplifyforandroid">
    <application
        android:name=".MyApplication" />
</manifest>

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
MFRamon-zzcommented, Feb 2, 2021

For whoever might be facing this issue right now, I’ve tried initializing the Amplify object from the Application class just as @jamesonwilliams says, the stability of the DataStore request and such on my app has gotten way better when only having this class as a singleton in the lifecycle of my app. instead of instantiating multiple object on each Activity

1reaction
TrekSoftcommented, Jan 3, 2020

Yeah I actually have the following language in the couple of documentation pages I wrote - perhaps we should adopt similar language throughout the rest of the documentation:

Add the following imports at the top of your MainActivity and code at the bottom of the onCreate method (ideally this would go in your Application class but this works for getting started quickly)

Read more comments on GitHub >

github_iconTop Results From Across the Web

android - Service onCreate called before Application onCreate
I can't tell you why your service is started before the Application 's onCreate was called, especially because there is a lot of...
Read more >
Handling Lifecycles with Lifecycle-Aware Components
This can cause a race condition where the onStop() method finishes before the onStart() , keeping the component alive longer than it's needed....
Read more >
Crashlytics Logs With 'Impossible' NullPointerExceptions
onCreate() will crash the app. Store data in the Bundle object. Another option would be to store the Profile in the Bundle object...
Read more >
Android Xamarin Forms set splash screen while ... - MSDN
Any examples would be much appreciated. ... the app launches and shows the splash screen... then crashes at OnCreate.
Read more >
Native Ads - Audience Network - Meta for Developers
onCreate (); // Initialize the Audience Network SDK ... @Override public void onAdLoaded(Ad ad) { // Race condition, load() called again before last...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found