You need to use a Theme.AppCompat theme (or descendant) with this activity in Flutter when opening an AAR

48 views Asked by At

I have an issue when I try to open an AAR module from Flutter side. I have implemented the method channel functions to open StartActivity of AAR but when I try to do it, it gives me this error:

E/AndroidRuntime(32503): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.appName.devFlavor/com.example.app.StartPageActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
E/AndroidRuntime(32503):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782)
E/AndroidRuntime(32503):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922)
E/AndroidRuntime(32503):        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
E/AndroidRuntime(32503):        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
E/AndroidRuntime(32503):        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
E/AndroidRuntime(32503):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
E/AndroidRuntime(32503):        at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(32503):        at android.os.Looper.loopOnce(Looper.java:205)
E/AndroidRuntime(32503):        at android.os.Looper.loop(Looper.java:294)
E/AndroidRuntime(32503):        at android.app.ActivityThread.main(ActivityThread.java:8177)
E/AndroidRuntime(32503):        at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(32503):        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
E/AndroidRuntime(32503):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
E/AndroidRuntime(32503): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
E/AndroidRuntime(32503):        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:852)
E/AndroidRuntime(32503):        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:815)
E/AndroidRuntime(32503):        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:703)
E/AndroidRuntime(32503):        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
E/AndroidRuntime(32503):        at com.example.app.StartPageActivity.onCreate(StartPageActivity.java:17)
E/AndroidRuntime(32503):        at android.app.Activity.performCreate(Activity.java:8595)
E/AndroidRuntime(32503):        at android.app.Activity.performCreate(Activity.java:8573)
E/AndroidRuntime(32503):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1456)
E/AndroidRuntime(32503):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3764)

So I did notice that Flutter uses different theming in styles.xm and I decided to use AppCompat but still I'm unable to open the StartActivity intent of AAR.

Here is my values/styles.xml

    <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:forceDarkAllowed">false</item>
        <item name="android:windowFullscreen">false</item>
        <item name="android:windowDrawsSystemBarBackgrounds">false</item>
        <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    </style> 

    <style name="AppFullScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

This is how I open the intent from method channels and My MainActivity.kt file:

package com.company.appName

import android.content.Intent
import io.flutter.embedding.android.FlutterActivity
import androidx.annotation.NonNull
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import android.content.Context

class MainActivity: FlutterActivity() {
     private val CHANNEL = "com.company.appName/aarchannel"
     private val AARMETHOD = "aarcall"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(
            flutterEngine.dartExecutor.binaryMessenger,
            CHANNEL
        ).setMethodCallHandler { call, result ->
            if (call.method == AARMETHOD) {
                openAAR(context,result)
            } else {
                result.notImplemented()
            }
        }
}

 private fun openAAR(context: Context, result : MethodChannel.Result){
                val intent = Intent(context, com.example.app.StartPageActivity::class.java)
                startActivity(intent)
                result.success(true)
  }

}

com.example.app is AAR's package name. It actually finds the package but when I try to open as an intent from Flutter side, it gives the error I have mentioned above.

Is there anything I can do to fix Appcompat issue?

0

There are 0 answers