OnActivityResult not triggered on finish

568 Views Asked by At

The following application works as expected:

import android.app.Activity
import android.content.Intent
import android.graphics.BitmapFactory
import android.app.WallpaperManager

class ChwallActivity < Activity
  def onCreate(state)
    super
    setContentView R.layout.main
  end

  $Override
  def onStart
    super
    Intent intent = Intent.new(Intent.ACTION_PICK)
    intent.setType "image/*"
    startActivityForResult Intent.createChooser(intent, "Select Picture"), 0
  end

  $Override
  def onActivityResult(requestCode, resultCode, data:Intent)
    super
    thumb = BitmapFactory.decodeFile "/storage/sdcard0/download/foo.jpg"
    manager = WallpaperManager.getInstance self
    manager.setBitmap thumb
  end
end

This executes a gallery-picker in an infinite loop, which is undesirable. However, if I insert a finish at the end of the onStart() function, onActivityResult() doesn't seem to be called: the wallpaper doesn't change to foo.jpg. Is onActivityResult() being called when the gallery starts up the second time? What is going on?

3

There are 3 best solutions below

0
On BEST ANSWER

Putting the finish in onActivityResult() works.

2
On

Move the following code to onCreate and it should work well

Intent intent = Intent.new(Intent.ACTION_PICK)
intent.setType "image/*"
startActivityForResult Intent.createChooser(intent, "Select Picture"), 0
0
On

OnStart() is called again when Activity becomes visible again, after picker Activity finishes, which starts picker Activity again, and so on.

You shouldn't put that logic in onStart() instead have a user event launch the picker.

OR

You could use a boolean flag to track if you've picked the picture already.