artagnon
artagnon

Reputation: 3719

OnActivityResult not triggered on finish

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

Answers (3)

s.d
s.d

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

artagnon
artagnon

Reputation: 3719

Putting the finish in onActivityResult() works.

Upvotes: 0

Vladimir Mironov
Vladimir Mironov

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

Related Questions