androidflutterflutter-add-to-app

Requested flutter engine does not exist in Add to app


I have flutter into the existing native app (Add to app) with a cached flutter engine.

override fun onCreate(savedInstanceState: Bundle?) {
        Log.d(TAG, "onCreate")
        
        
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)
        context = this.application.applicationContext
        if (!FlutterEngineCache.getInstance().contains(
                FLUTTER_ENGINE
            )
        ) {
            mFlutterEngine = FlutterEngine(context)
            mFlutterEngine.dartExecutor
                .executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
            FlutterEngineCache.getInstance()
                .put(FLUTTER_ENGINE, mFlutterEngine)
        }
        //Create Flutter Fragment
        mFragmentManager = supportFragmentManager
        mFlutterFragment = mFragmentManager.findFragmentByTag(FLUTTER_FRAGMENT) as FlutterFragment?
        if (mFlutterFragment == null) {
            mFlutterFragment =
                FlutterFragment.withCachedEngine(FLUTTER_ENGINE).transparencyMode(FlutterView.TransparencyMode.opaque).build()
            mFragmentManager
                .beginTransaction()
                .add(R.id.fragment_container, mFlutterFragment as Fragment, FLUTTER_FRAGMENT)
                .commit()
        } else {
            mFragmentManager
                .beginTransaction()
                .show(mFragmentManager.findFragmentByTag(FLUTTER_FRAGMENT)!!)
                .commit()
        }
}

I am getting the following crash in the production app

  Caused by: java.lang.IllegalStateException: The requested cached FlutterEngine did not exist in the FlutterEngineCache: 'FLUTTER_ENGINE'
        at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.setupFlutterEngine(FlutterActivityAndFragmentDelegate.java:211)
        at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onAttach(FlutterActivityAndFragmentDelegate.java:157)
        at io.flutter.embedding.android.FlutterFragment.onAttach(FlutterFragment.java:597)
        at androidx.fragment.app.Fragment.performAttach(Fragment.java:2574)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:828)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
        at androidx.fragment.app.FragmentManagerImpl.dispatchCreate(FragmentManagerImpl.java:2607)
        at androidx.fragment.app.FragmentController.dispatchCreate(FragmentController.java:235)
        at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:316)
        at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:106)
        at com.flutter.android.MainActivity.onCreate(MainActivity.kt:33)
 

Solution

  • How to reproduce

    1. Open developer settings, set "Background processes" to "No Background Processes" and set on the Don't keep activities.

    2. Open the app press and home

    3. Open the app again .. it will crash with the exception.

      Caused by: java.lang.IllegalStateException: The requested cached FlutterEngine did not exist in the FlutterEngineCache: 'FLUTTER_ENGINE'

    Solution

    initialize the flutter engine before super.oncreate()

    override fun onCreate(savedInstanceState: Bundle?) {
            Log.d(TAG, "onCreate")
             context = this.application.applicationContext
            if (!FlutterEngineCache.getInstance().contains(
                    FLUTTER_ENGINE
                )
            ) {
                mFlutterEngine = FlutterEngine(context)
                mFlutterEngine.dartExecutor
                    .executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
                FlutterEngineCache.getInstance()
                    .put(FLUTTER_ENGINE, mFlutterEngine)
            }
           
            
            super.onCreate(savedInstanceState)
    
            setContentView(R.layout.activity_main)
            //Create Flutter Fragment
            mFragmentManager = supportFragmentManager
            mFlutterFragment = mFragmentManager.findFragmentByTag(FLUTTER_FRAGMENT) as FlutterFragment?
            if (mFlutterFragment == null) {
                mFlutterFragment =
                    FlutterFragment.withCachedEngine(FLUTTER_ENGINE).transparencyMode(FlutterView.TransparencyMode.opaque).build()
                mFragmentManager
                    .beginTransaction()
                    .add(R.id.fragment_container, mFlutterFragment as Fragment, FLUTTER_FRAGMENT)
                    .commit()
            } else {
                mFragmentManager
                    .beginTransaction()
                    .show(mFragmentManager.findFragmentByTag(FLUTTER_FRAGMENT)!!)
                    .commit()
            }