title: Flutter Android ERROR: ensureInitializationComplete must be called after startInitialization
body: <p>Currently I'm trying to add a flutter app to my android app but when I try to do an Intent the app crashes and prints the following issue:</p>
  
  <p>I followed this guide <a href="https://github.com/flutter/flutter/wiki/Experimental:-Add-Flutter-Activity" rel="noreferrer">here</a> 
  <br></p>
  
  <p>I get this error: </p>
  
  <pre><code>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testappandroid/io.flutter.embedding.android.FlutterActivity}: java.lang.IllegalStateException: ensureInitializationComplete must be called after startInitialization
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
          at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
          at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
          at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
          at android.os.Handler.dispatchMessage(Handler.java:106)
          at android.os.Looper.loop(Looper.java:193)
          at android.app.ActivityThread.main(ActivityThread.java:6669)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
       Caused by: java.lang.IllegalStateException: ensureInitializationComplete must be called after startInitialization
          at io.flutter.view.FlutterMain.ensureInitializationComplete(FlutterMain.java:168)
          at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.initializeFlutter(FlutterActivityAndFragmentDelegate.java:177)
          at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onAttach(FlutterActivityAndFragmentDelegate.java:140)
          at io.flutter.embedding.android.FlutterActivity.onCreate(FlutterActivity.java:399)
          at android.app.Activity.performCreate(Activity.java:7136)
          at android.app.Activity.performCreate(Activity.java:7127)
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
  </code></pre>
  
  <p>So it says: ensureInitializationComplete must be called after startInitialization. One solution is to call FlutterMain.startInitialization(this) in the Flutter Activity. But this isn't working for me:</p>
  
  <p>My Flutter Activity:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class MainActivity extends FlutterActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
  FlutterMain.startInitialization(this);
      super.onCreate(savedInstanceState);
      GeneratedPluginRegistrant.registerWith(this);
    }
  }
  </code></pre>
  
  <p>My android app AndroidManifest:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      package="com.example.testappandroid"&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:roundIcon="@mipmap/ic_launcher_round"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"
          tools:ignore="GoogleAppIndexingWarning"&gt;
          &lt;activity android:name=".MainActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name="io.flutter.embedding.android.FlutterActivity"
              android:theme="@style/AppTheme"
              android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
              android:hardwareAccelerated="true"
              android:windowSoftInputMode="adjustResize"
              android:exported="true"&gt;
              &lt;meta-data
                  android:name="io.flutter.Entrypoint"
                  android:value="main"
                  /&gt;
          &lt;/activity&gt;
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>My flutter module AndroidManifest:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.example.testappflutter.host"&gt;
  
      &lt;!-- The INTERNET permission is required for development. Specifically,
           flutter needs it to communicate with the running application
           to allow setting breakpoints, to provide hot reload, etc.
      --&gt;
      &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  
      &lt;!-- io.flutter.app.FlutterApplication is an android.app.Application that
           calls FlutterMain.startInitialization(this); in its onCreate method.
           In most cases you can leave this as-is, but you if you want to provide
           additional functionality it is fine to subclass or reimplement
           FlutterApplication and put your custom class here. --&gt;
      &lt;application
          android:name="io.flutter.app.FlutterApplication"
          android:label="testappflutter"
          android:icon="@mipmap/ic_launcher"&gt;
          &lt;activity
              android:name=".MainActivity"
              android:launchMode="singleTop"
              android:theme="@style/LaunchTheme"
              android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
              android:hardwareAccelerated="true"
              android:windowSoftInputMode="adjustResize"&gt;
              &lt;!-- This keeps the window background of the activity showing
                   until Flutter renders its first frame. It can be removed if
                   there is no splash screen (such as the default splash screen
                   defined in @style/LaunchTheme). --&gt;
              &lt;meta-data
                  android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                  android:value="true" /&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN"/&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER"/&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  
  </code></pre>
  
  <p>With this line of code I try to open the FlutterActivtiy. This is calling the main() function in my dart code:</p>
  
  <pre class="lang-java prettyprint-override"><code>Intent defaultFlutter = createDefaultIntent(activity);
  activity.startActivity(defaultFlutter);
  
  </code></pre>
  
  <p>The code above should start this:</p>
  
  <pre class="lang-dart prettyprint-override"><code>void main() =&gt; runApp(MaterialApp(
    routes: {
      "/": (context) =&gt; TestWidget()
    }
  ));
  </code></pre>
  
  <p>So I followed step by step the guide for adding Flutter to an existing android app. Do I forget something? In my mind maybe the AndroidManifest of the flutter app needs some changes? Any ideas what I'm doing wrong?</p>