PspdfkitWidget AppCompatActivity issue
The PspdfkitWidget
for Flutter requires the MainActivity
in the Flutter Android project to extend AppCompatActivity
, but currently, none of the available Flutter Activity
implementations extend AppCompatActivity
. This leads to a crash when you use it in your application:
java.lang.ClassCastException: com.example.app.MainActivity cannot be cast to androidx.appcompat.app.AppCompatActivity
You can fix this by extending our custom implementation of FlutterAppCompatActivity
instead. You might see compile errors if androidx.appcompat:appcompat
isn’t included in your dependencies, so make sure the android/app/build.gradle
file has the line below under dependencies, and replace $appcompat_version
with an actual version:
implementation "androidx.appcompat:appcompat:$appcompat_version"
Using FlutterAppCompatActivity
also introduces another issue if your application’s theme isn’t AppCompat
based:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activitylifecycletest/com.example.activitylifecycletest.DialogActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
Fix this by changing your Android project’s main theme parent to an AppCompat
-based theme. We recommend using PSPDFKit.Theme.Default
.
Go to /android/app/src/main/res/values/styles.xml
and make the following changes:
<style name="NormalTheme" parent="@style/PSPDFKit.Theme.Default"> ...... </style>
For dark mode support, set the parent theme in the values-night/styles.xml
file. We recommend using the PSPDFKit.Theme.Dark
theme. Go to /android/app/src/main/res/values-night/styles.xml
and make the following changes:
<style name="NormalTheme" parent="@style/PSPDFKit.Theme.Dark"> ...... </style>
If the night mode theme isn’t updated, the app will crash when running on phones in dark mode. If your app doesn’t support dark mode, set the parent theme to
PSPDFKit.Theme.Default
for consistency.