OnActivityResult未在完成时触发

时间:2013-02-15 15:40:51

标签: android android-activity mirah

以下应用程序按预期工作:

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

这会在无限循环中执行一个gallery-picker,这是不可取的。但是,如果我在finish函数的末尾插入onStart(),则似乎不会调用onActivityResult():壁纸不会更改为foo.jpg。画廊第二次启动时是否会调用onActivityResult()?发生了什么事?

3 个答案:

答案 0 :(得分:2)

将以下代码移至onCreate,它应该可以正常运行

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

答案 1 :(得分:0)

finish放在onActivityResult()中。

答案 2 :(得分:0)

当“选择器活动”完成后,“活动”再次可见时再次调用

OnStart(),再次启动选择器“活动”,依此类推。

您不应该将该逻辑放在onStart()中,而是让用户事件启动选择器。

OR

您可以使用boolean标记来跟踪您是否已经选择了图片。