classMyActivityextendsActivitywithTypedFindView{lazyvalIMAGE_FETCH_ID=12345overridedefonCreate(bundle:Bundle){//Activity初期化処理など//...findView(TR.button_image).onClick{_=>valintent=newIntent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)intent.setType("image/*")startActivityForResult(intent,IMAGE_FETCH_ID)//データパイプラインがここで分断}}overridedefonActivityResult(requestCode:Int,resultCode:Int,data:Intent):Unit={requestCodematch{caseIMAGE_FETCH_ID=>//データパイプラインがここから継続if(resultCode==Activity.RESULT_OK)findView(TR.image).setImageURI(data.getData)else{//failed. Do something if needed. }case_=>//do nothing}}}
traitImageLoadableextendsActivity{lazyvalIMAGE_FETCH_ID=12345privatevarpromise:Option[Promise[Uri]]=NonedefchooseImageUri():Future[Uri]={promise.filterNot(_.isCompleted).foreach(_.failure(newInterruptedException("Asked to load another image. Aborted.")))valp=Promise[Uri]()promise=Some(p)valintent=newIntent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)intent.setType("image/*")startActivityForResult(intent,IMAGE_FETCH_ID)p.future}overridedefonActivityResult(requestCode:Int,resultCode:Int,data:Intent):Unit={super.onActivityResult(requestCode,resultCode,data)requestCodematch{caseIMAGE_FETCH_ID=>if(resultCode==Activity.RESULT_OK)promise.foreach(_.success(data.getData))elsepromise.foreach(_.failure(ImageNotAvailableException("Failed to fetch image.")))case_=>//do nothing}}}