changeset 2496:3bf2f08fdc45

Android: Remove ugly workaround for initialization issues. Solved by a change to AndroidManifest.xml to prevent recreation on configuration changes. Add some code to detect the changes.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 03 May 2021 07:15:11 +0000
parents 5664c91d03fb
children 7b210f156eef
files android/AndroidManifest.xml android/DWindows.kt android/dw.cpp
diffstat 3 files changed, 30 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/android/AndroidManifest.xml	Mon May 03 01:17:40 2021 +0000
+++ b/android/AndroidManifest.xml	Mon May 03 07:15:11 2021 +0000
@@ -9,7 +9,8 @@
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
         android:theme="@style/Theme.Dwtest">
-        <activity android:name=".DWindows">
+        <activity android:name=".DWindows"
+            android:configChanges="orientation|screenSize|screenLayout|keyboardHidden">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
 
--- a/android/DWindows.kt	Mon May 03 01:17:40 2021 +0000
+++ b/android/DWindows.kt	Mon May 03 07:15:11 2021 +0000
@@ -4,6 +4,7 @@
 import android.content.ClipboardManager
 import android.content.DialogInterface
 import android.content.pm.ActivityInfo
+import android.content.res.Configuration
 import android.graphics.drawable.GradientDrawable
 import android.media.AudioManager
 import android.media.ToneGenerator
@@ -57,15 +58,33 @@
         // Turn on rotation
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR)
 
-        // Get the Android app path
-        val m = packageManager
-        var s = packageName
-        val p = m.getPackageInfo(s!!, 0)
-        s = p.applicationInfo.dataDir
+        // We only want to call this once when the app starts up
+        // By default Android will call onCreate for rotation and other
+        // changes.  This is incompatible with Dynamic Windows...
+        // Make sure the following is in your AndroidManifest.xml
+        // android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
+        if(savedInstanceState == null) {
+            // Get the Android app path
+            val m = packageManager
+            var s = packageName
+            val p = m.getPackageInfo(s!!, 0)
+            s = p.applicationInfo.dataDir
 
-        // Initialize the Dynamic Windows code...
-        // This will start a new thread that calls the app's dwmain()
-        dwindowsInit(s)
+            // Initialize the Dynamic Windows code...
+            // This will start a new thread that calls the app's dwmain()
+            dwindowsInit(s)
+        }
+    }
+
+    override fun onConfigurationChanged(newConfig: Configuration) {
+        super.onConfigurationChanged(newConfig)
+
+        // Checks the orientation of the screen
+        if (newConfig.orientation === Configuration.ORIENTATION_LANDSCAPE) {
+            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show()
+        } else if (newConfig.orientation === Configuration.ORIENTATION_PORTRAIT) {
+            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show()
+        }
     }
 
     /*
--- a/android/dw.cpp	Mon May 03 01:17:40 2021 +0000
+++ b/android/dw.cpp	Mon May 03 07:15:11 2021 +0000
@@ -89,15 +89,8 @@
 {
     static int runcount = 0;
 
-    /* Safety check to prevent multiple initializations...
-     * In the simulator I get multiple calls, and code only works on the second call.
-     * On actual hardware we only get called once... so this works around that.
-     */
-#if defined(__arm__) || defined(__aarch64__)
+    /* Safety check to prevent multiple initializations... */
     if(runcount == 0)
-#else
-    if(runcount == 1)
-#endif
     {
         char *arg = strdup(env->GetStringUTFChars((jstring) path, NULL));