changeset 3:21d2df45d350

Fix race condition.
author David Barts <n5jrn@me.com>
date Tue, 09 Feb 2021 08:56:00 -0800
parents 06825e49f7aa
children b6a217c850fb
files app/src/main/java/com/bartsent/simpleresizer/EditImage.kt app/src/main/java/com/bartsent/simpleresizer/MainActivity.kt
diffstat 2 files changed, 16 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Sun Feb 07 21:08:34 2021 -0800
+++ b/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Tue Feb 09 08:56:00 2021 -0800
@@ -10,6 +10,7 @@
 import android.os.Environment
 import android.provider.MediaStore
 import android.provider.OpenableColumns
+import android.util.Log
 import android.view.MenuItem
 import android.view.View
 import android.widget.EditText
@@ -36,6 +37,7 @@
         super.onCreate(savedInstanceState)
         binding = ActivityEditImageBinding.inflate(layoutInflater)
         setContentView(binding.root)
+        Log.d("EditImage", "onCreate called")
     }
 
     // Cribbed from: https://stackoverflow.com/questions/5568874/how-to-extract-the-file-name-from-uri-returned-from-intent-action-get-content
@@ -69,6 +71,7 @@
 
     override fun onResume() {
         super.onResume()
+        Log.d("EditImage", "onResume called")
 
         // Read the URI, die if we can't.
         val imageUri = intent?.data
--- a/app/src/main/java/com/bartsent/simpleresizer/MainActivity.kt	Sun Feb 07 21:08:34 2021 -0800
+++ b/app/src/main/java/com/bartsent/simpleresizer/MainActivity.kt	Tue Feb 09 08:56:00 2021 -0800
@@ -11,27 +11,34 @@
 class MainActivity : AppCompatActivity() {
     val GET_IMAGE = 1
     private lateinit var binding: ActivityMainBinding
+    private var showChooser = true
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
-        setContentView(R.layout.activity_main)
         binding = ActivityMainBinding.inflate(layoutInflater)
+        setContentView(binding.root)
     }
 
     override fun onResume() {
         super.onResume()
-        Intent(Intent.ACTION_GET_CONTENT).run {
-            type = "image/*"
-            addCategory(Intent.CATEGORY_OPENABLE)
-            startActivityForResult(this, GET_IMAGE)
+        if (showChooser) {
+            Intent(Intent.ACTION_GET_CONTENT).run {
+                type = "image/*"
+                addCategory(Intent.CATEGORY_OPENABLE)
+                startActivityForResult(this, GET_IMAGE)
+            }
+        } else {
+            showChooser = true
         }
     }
 
     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
+        showChooser = false
         super.onActivityResult(requestCode, resultCode, data)
-        Log.d("MainActivity", "onActivityResult called")
+        Log.d("MainActivity", "onActivityResult called, requestCode=$requestCode, resultCode=$resultCode")
         if (requestCode == GET_IMAGE) {
             val imageUri = data?.data
+            Log.d("MainActivity", "imageUri=${imageUri ?: "(null)"}")
             if (resultCode == Activity.RESULT_OK && imageUri != null) {
                 Intent(Intent.ACTION_SEND, imageUri, this, EditImage::class.java).run {
                     startActivity(this)