Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Do you know onActivityResult?

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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) { // ... } } }

Slide 7

Slide 7 text

๭ϑϦϚΞϓϦ͔Βഈआͨ͠
 onActivityResult ※ఆ਺໊ͱ͔ͦ͜Βล͸ม͑ͯ·͢

Slide 8

Slide 8 text

๭ϑϦϚΞϓϦ 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; } }

Slide 9

Slide 9 text

@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

Slide 10

Slide 10 text

@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

Slide 11

Slide 11 text

@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

Slide 12

Slide 12 text

@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

Slide 13

Slide 13 text

@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

Slide 14

Slide 14 text

onActivityResultͷѱ͞ • switchͱif͕ଟ͍ • requestCodeͱresultCodeΛνΣοΫ͠ͳ͍ͱ͍͚ͳ͍ • switchͱifͷॻ͖ํ͕·ͪ·ͪ • switch࢖Θͳ͍੎ + if else੎ • requestCodeͱresultCodeͲͬͪʹઌʹ൑ఆ͢Δ͔໰୊ • ਓ͕ؒॻ͘ʹ͸ͭΒ͍...

Slide 15

Slide 15 text

Do you know OnActivityResult?

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

onActivityResultͷ࣮૷͕͜Ε͚ͩʂʂ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); ActivityResult.onResult(requestCode, resultCode, data).into(this); }

Slide 19

Slide 19 text

͗͢ΐ͍

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

ड͚औͬͨ͋ͱͷ࣮૷͸ʁʁ @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(); }

Slide 22

Slide 22 text

ड͚औͬͨ͋ͱͷ࣮૷͸ʁʁ // 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(); }

Slide 23

Slide 23 text

͗͢ΐ͘ͳ͍ʁ

Slide 24

Slide 24 text

(ΈΜͳ) ͗͢ΐ͍

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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 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()); } }

Slide 27

Slide 27 text

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Ͱ࡞੒͞ΕΔίʔυ΋
 ೖΕͯ͋Γ·͢

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Ͳ͏͍͏৔߹ʹ࢖͏͔ • ը໘A͔Βը໘B΁RequestΛ͢Δ࣌
 ը໘B͔Βͷ໭Γ͕2ύλʔϯҎ্͋Δ ৔߹ • ???

Slide 30

Slide 30 text

Ξοςͷྫ)
 ืू౤ߘը໘͔Βը૾બ୒Λ͢Δ • ը૾બ୒ը໘Ͱ͸ΪϟϥϦʔͷը૾Λબ୒͢Δ͔ɺΧϝϥͰࡱӨ͢Δ͔ͷ
 2ύλʔϯ͋Δ • ΧϝϥࡱӨ͸·ͨผը໘͕ىಈͯ͠ɺͦͷ໭ΓΛը૾બ୒ը໘͕ड͚औΔ • ืू౤ߘը໘΁ͷ໭Γ͕ɺΪϟϥϦʔͷը૾Λબ୒ɺΧϝϥͰࡱӨͷ2ύλʔϯ͋Δ • = ืू౤ߘը໘͔Βը૾બ୒ը໘ʹରͯ͠͸1Request͚ͩͲ
 2ύλʔϯͷ໭Γ(Resultύλʔϯ)͕͋Δ • = ಠࣗͷResult CodeΛఆ͍ٛͨ͠ = Activity.RESULT_FIRST_USERΛ࢖ͬͯఆٛ

Slide 31

Slide 31 text

ΞοςͷྫͰ...

Slide 32

Slide 32 text

ΞοςͷྫͰ...

Slide 33

Slide 33 text

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 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(); }

Slide 34

Slide 34 text

Support user defined result code https://github.com/vanniktech/ OnActivityResult/pull/86 Զ͕ʂ OnActivityResultͰActivity.RESULT_FIRST_USER ࢖͑ΔΑ͏ʹͯ͠΍ͬͨͧʂ

Slide 35

Slide 35 text

Contributions

Slide 36

Slide 36 text

Contributions

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

͓·͑Βʂ ΋͏onActivityResultͰ
 switchͱifॻ࣌͘୅͸
 ऴΘΓͩͧʂ

Slide 40

Slide 40 text

Thanks!!