OnActivityResult - おまえら!もうonActivityResultでswitchとif書く時代は終わりだぞ!

E77b6a5f919f7366d94f21eee9a014f5?s=47 operandoOS
January 20, 2017

OnActivityResult - おまえら!もうonActivityResultでswitchとif書く時代は終わりだぞ!

OnActivityResult
おまえら!もうonActivityResultでswitchとif書く時代は終わりだぞ!

OnActivityResult
https://github.com/vanniktech/OnActivityResult

Sample Code
https://github.com/operando/OnActivityResult/tree/my_sample_code/onactivityresult-sample

Activity.RESULT_FIRST_USER
https://developer.android.com/reference/android/app/Activity.html#RESULT_FIRST_USER

kyobashi.dex #4
https://rmp-quipper.connpass.com/event/47555/

E77b6a5f919f7366d94f21eee9a014f5?s=128

operandoOS

January 20, 2017
Tweet

Transcript

  1. OnActivityResult kyobashi.dex #4 ͓·͑Βʂ΋͏onActivityResultͰswitchͱifॻ࣌͘୅͸ऴΘΓͩͧʂ

  2. About Me Shinobu Okano @operandoOS Mercari, Inc. Souzoh, Inc.

  3. None
  4. Do you know onActivityResult?

  5. Do you know onActivityResult? https://developer.android.com/ training/basics/intents/result.html Getting a Result from

    an Activity
  6. Getting a Result from an Activity // Start the Activity

    static final int PICK_CONTACT_REQUEST = 1; // The request code private void pickContact() { Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts")); pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST); } // Receive the Result @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PICK_CONTACT_REQUEST) { if (resultCode == RESULT_OK) { // ... } } }
  7. ๭ϑϦϚΞϓϦ͔Βഈआͨ͠
 onActivityResult ※ఆ਺໊ͱ͔ͦ͜Βล͸ม͑ͯ·͢

  8. ๭ϑϦϚΞϓϦ onActivityResult @Override protected void onActivityResult(int requestCode, int resultCode, Intent

    data) { super.onActivityResult(requestCode, resultCode, data); switch(requestCode) { case LOGIN: if(resultCode == RESULT_OK) { success(); } else { failed(); } break; } }
  9. @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data); if(resultCode != RESULT_OK) return; switch (requestCode) { case REQUEST_MUGAMUGA: successItemPost(); break; } } ๭ϑϦϚΞϓϦ onActivityResult
  10. @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data); if (resultCode != RESULT_OK || data == null) { mugemuge(); return; } if (requestCode == Request.UMUUMU) { umuumu(); } } ๭ϑϦϚΞϓϦ onActivityResult
  11. @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == Activity.RESULT_CANCELED) { cancel(); } else if (requestCode == REQUEST_CODE_SELECT_IMAGE) { // .... } else if (requestCode == REQUEST_CODE_TRIMMING) { // .... } else { super.onActivityResult(requestCode, resultCode, data); } } ๭ϑϦϚΞϓϦ onActivityResult
  12. @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_MUGAMUGA && resultCode == Activity.RESULT_OK) { if (data != null) { mugamuga(); } } } ๭ϑϦϚΞϓϦ onActivityResult
  13. @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) { return; } switch (requestCode) { case REQUEST_CODE_CATEGORY: // ... break; case REQUEST_CODE_LOCATION: // ... break; case REQUEST_CODE_CONDITION: // ... break; default: super.onActivityResult(requestCode, resultCode, data); break; } } ๭ϑϦϚΞϓϦ onActivityResult
  14. onActivityResultͷѱ͞ • switchͱif͕ଟ͍ • requestCodeͱresultCodeΛνΣοΫ͠ͳ͍ͱ͍͚ͳ͍ • switchͱifͷॻ͖ํ͕·ͪ·ͪ • switch࢖Θͳ͍੎ +

    if else੎ • requestCodeͱresultCodeͲͬͪʹઌʹ൑ఆ͢Δ͔໰୊ • ਓ͕ؒॻ͘ʹ͸ͭΒ͍...
  15. Do you know OnActivityResult?

  16. OnActivityResult OnActivityResult annotation compiler for Android https://github.com/vanniktech/OnActivityResult

  17. Ξοςͷ
 onActivityResultͷ࣮૷͸
 ͢΂ͯOnActivityResultͰ͢ʂ

  18. onActivityResultͷ࣮૷͕͜Ε͚ͩʂʂ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data)

    { super.onActivityResult(requestCode, resultCode, data); ActivityResult.onResult(requestCode, resultCode, data).into(this); }
  19. ͗͢ΐ͍

  20. ड͚औͬͨ͋ͱͷ࣮૷͸ʁʁ • ͦΕͧͷͨύλʔϯʹ͋ͬͨϝιουΛఆٛͯ͠ @OnActivityResultΛ͚ͭΔ • @OnActivityResultͷrequestCodeͱresultCodesͰ
 Ͳ͏͍͏݁Ռͷ৔߹ʹݺͼग़ͯ͠΄͍͔͠Λఆٛ͢Δ

  21. ड͚औͬͨ͋ͱͷ࣮૷͸ʁʁ @OnActivityResult(requestCode = REQUEST_TAKE_PHOTO, resultCodes = RESULT_OK) void onTakePhoto(@ExtraString(name =

    CameraActivity.RESULT_TAKE_PHOTO) final String takePhoto) { Intent resultIntent = new Intent(); resultIntent.putExtra(RESULT_TAKE_PHOTO, takePhoto); setResult(RESULT_CODE_TAKE_PHOTO, resultIntent); finish(); }
  22. ड͚औͬͨ͋ͱͷ࣮૷͸ʁʁ // result codeʹԿ͕ฦ͖ͬͯͯ΋ॲཧ͢Δϝιου @OnActivityResult(requestCode = REQUEST_SELECT_IMAGE) void onHumuHumu() {

    Toast.makeText(this, "onHumuHumu", Toast.LENGTH_SHORT).show(); } @OnActivityResult(requestCode = REQUEST_SELECT_IMAGE, resultCodes = RESULT_CANCELED) void onCancel() { Toast.makeText(this, "cancel select image.”, Toast.LENGTH_SHORT).show(); }
  23. ͗͢ΐ͘ͳ͍ʁ

  24. (ΈΜͳ) ͗͢ΐ͍

  25. onActivityResultͷड͚औΓઌͷ ΦϒδΣΫτΛࢦఆ͢Δ • intoʹड͚औΓઌΦϒδΣΫτΛࢦఆ͢Δ • ActivityResult.onResult().into(obj); • onActivityResultͰฦͬͯ͘ΔIntentͷσʔλΛ ViewModelͱ͔PresenterͰड͚औΔ •

    ઃܭʹΑͬͯ͸خ͍͠
  26. onActivityResultͷ݁ՌΛ
 ViewModelͰड͚औΔ private MySampleMainViewModel viewModel = new MySampleMainViewModel(); @Override protected

    void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); ActivityResult.onResult(requestCode, resultCode, data).into(viewModel); } public class MySampleMainViewModel { public ObservableField<String> location = new ObservableField<>("0,0"); @OnActivityResult(requestCode = MySampleMainActivity.REQUEST_SELECT_LOCATION, resultCodes = Activity.RESULT_OK) void onSelectedLocation(@Extra(name = LocationSelectActivity.RESULT_SELECT_LOCATION) Location location) { this.location.set(location.toString()); } }
  27. OnActivityResult sample Code https://github.com/operando/OnActivityResult/tree/ my_sample_code/onactivityresult-sample https://github.com/operando/OnActivityResult/tree/my_sample_code/onactivityresult-sample/ build/generated/source/apt/debug/com/vanniktech/onactivityresult/sample ෍ڭ׆ಈͷͨΊʹద౰ʹαϯϓϧίʔυॻ͍ͯ·͢ annotation processorͰ࡞੒͞ΕΔίʔυ΋


    ೖΕͯ͋Γ·͢
  28. Do you know Activity.RESULT_FIRST_USER? • Start of user-defined activity results.

    • Constant Value: 1 (0x00000001) • https://developer.android.com/reference/android/ app/Activity.html#RESULT_FIRST_USER
  29. Ͳ͏͍͏৔߹ʹ࢖͏͔ • ը໘A͔Βը໘B΁RequestΛ͢Δ࣌
 ը໘B͔Βͷ໭Γ͕2ύλʔϯҎ্͋Δ ৔߹ • ???

  30. Ξοςͷྫ)
 ืू౤ߘը໘͔Βը૾બ୒Λ͢Δ • ը૾બ୒ը໘Ͱ͸ΪϟϥϦʔͷը૾Λબ୒͢Δ͔ɺΧϝϥͰࡱӨ͢Δ͔ͷ
 2ύλʔϯ͋Δ • ΧϝϥࡱӨ͸·ͨผը໘͕ىಈͯ͠ɺͦͷ໭ΓΛը૾બ୒ը໘͕ड͚औΔ • ืू౤ߘը໘΁ͷ໭Γ͕ɺΪϟϥϦʔͷը૾Λબ୒ɺΧϝϥͰࡱӨͷ2ύλʔϯ͋Δ •

    = ืू౤ߘը໘͔Βը૾બ୒ը໘ʹରͯ͠͸1Request͚ͩͲ
 2ύλʔϯͷ໭Γ(Resultύλʔϯ)͕͋Δ • = ಠࣗͷResult CodeΛఆ͍ٛͨ͠ = Activity.RESULT_FIRST_USERΛ࢖ͬͯఆٛ
  31. ΞοςͷྫͰ...

  32. ΞοςͷྫͰ...

  33. OnActivityResultͰ࣮૷͢Δͱ… // ImageSelectActivity static final int RESULT_CODE_SELECT_IMAGE = Activity.RESULT_FIRST_USER; static

    final int RESULT_CODE_TAKE_PHOTO = Activity.RESULT_FIRST_USER + 1; @OnActivityResult(requestCode = REQUEST_SELECT_IMAGE, resultCodes = ImageSelectActivity.RESULT_CODE_SELECT_IMAGE) void onSelectImage(Intent intent) { ArrayList<String> selectImages = intent.getStringArrayListExtra(RESULT_SELECT_IMAGES); Toast.makeText(this, "onSelectImage selectImages : " + selectImages, Toast.LENGTH_SHORT).show(); } @OnActivityResult(requestCode = REQUEST_SELECT_IMAGE, resultCodes = ImageSelectActivity.RESULT_CODE_TAKE_PHOTO) void onTakePhoto(@ExtraString(name = ImageSelectActivity.RESULT_TAKE_PHOTO) final String takePhoto) { Toast.makeText(this, "onTakePhoto takePhoto : " + takePhoto, Toast.LENGTH_SHORT).show(); }
  34. Support user defined result code https://github.com/vanniktech/ OnActivityResult/pull/86 Զ͕ʂ OnActivityResultͰActivity.RESULT_FIRST_USER ࢖͑ΔΑ͏ʹͯ͠΍ͬͨͧʂ

  35. Contributions

  36. Contributions

  37. Contributions ΠϠοοϗΥΥΥ ΦΦΥΦ΢ʂ

  38. OnActivityResultͷؾʹೖͬͯΔͱ͜Ζ • onActivityResultϝιουͷ࣮૷͸γϯϓϧʹͳΔʂ • startActivityForResultͷݺͼग़͠͸طଘͷ··Ͱ͍͍ʂ • ड͚औΓޙͷॲཧ͝ͱʹϝιουఆٛ͢ΔͷͰݟ௨͕͍͍͠ • ݁Ռड͚औΓઌͷΦϒδΣΫτΛࢦఆͰ͖Δ →

    into() • ifͱswitch͔Βͷ։์…
  39. ͓·͑Βʂ ΋͏onActivityResultͰ
 switchͱifॻ࣌͘୅͸
 ऴΘΓͩͧʂ

  40. Thanks!!