Reputation: 3719
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?
Upvotes: 1
Views: 563
Reputation: 29436
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.
Upvotes: 0
Reputation: 30874
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
Upvotes: 2