Upgrade to Pro — share decks privately, control downloads, hide ads and more …

OnActivityResult - おまえら!もうonActivityResultでswit...

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/

operandoOS

January 20, 2017
Tweet

More Decks by operandoOS

Other Decks in Technology

Transcript

  1. 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) { // ... } } }
  2. ๭ϑϦϚΞϓϦ 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; } }
  3. @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
  4. @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
  5. @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
  6. @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
  7. @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
  8. onActivityResultͷ࣮૷͕͜Ε͚ͩʂʂ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data)

    { super.onActivityResult(requestCode, resultCode, data); ActivityResult.onResult(requestCode, resultCode, data).into(this); }
  9. ड͚औͬͨ͋ͱͷ࣮૷͸ʁʁ @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(); }
  10. ड͚औͬͨ͋ͱͷ࣮૷͸ʁʁ // 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(); }
  11. 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()); } }
  12. 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
  13. 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(); }