Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Activity, Fragment, CustomView の使い分け - マッチョなActivityにさよならする方法 -

43,587 views

Published on

DroidKaigi

Published in: Technology
  • Be the first to comment

Activity, Fragment, CustomView の使い分け - マッチョなActivityにさよならする方法 -

  1. 1. あんざいゆき, @yanzm, uPhyca Inc.
  2. 2. 開発規模 成熟度 2008 2015
  3. 3. 開発規模 成熟度 2008 2015 コンタクトアプリのJavaファイル数 2.3
  4. 4. 開発規模 成熟度 2008 2015 Tablet
  5. 5. 開発規模 成熟度 2008 2015 コンタクトアプリのJavaファイル数 4.0.1 2.3 Tablet
  6. 6. 開発規模 成熟度 2008 2015 Material Design Tablet
  7. 7. 開発規模 成熟度 2008 2015 Tablet コンタクトアプリのJavaファイル数 4.0.1 2.3 5.0.1 Material Design
  8. 8. Activity Fragment CustomView テーマ ⚪ △ Windowを 作成 ⚪ 他の画面から 呼び出せる ⚪ ライフサイクル のハンドリング ⚪ ⚪ △
  9. 9. Activity Fragment CustomView 1画面に複数 ⚪ ⚪ Orientation 指定 ⚪ Fragmentを 持てる ⚪ 画面回転の前後 でインスタンス を保持 ⚪
  10. 10. Activity Fragment CustomView 描画 ⚪ KeyEvent ⚪ ⚪ バックキー ⚪ ⚪ BackStack ⚪ ⚪
  11. 11. Activity Fragment CustomView Options Menu ⚪ ⚪ Loader ⚪ ⚪ 状態の リストア ⚪ ⚪ △ テスト しやすさ ⚪
  12. 12. Activity Fragment CustomView ActionBar カスタマイズ ⚪ △
  13. 13. • ImageView • EditText • Spinner • RadioButton • pick list
  14. 14. private static final String KEY_DESSERT_LIST = "dessert_list"; @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); ArrayList<String> dessertList = new ArrayList<>(); … outState.putStringArrayList(KEY_DESSERT_LIST, dessertList); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); ArrayList<String> dessertList = savedInstanceState .getStringArrayList(KEY_DESSERT_LIST); … }
  15. 15. CompoundButtonのコードがわかりやすい
  16. 16. static class SavedState extends BaseSavedState { ArrayList<String> favoriteDessert; SavedState(Parcelable superState) { super(superState); } private SavedState(Parcel in) { super(in); favoriteDessert = in.readArrayList(String.class.getClassL } @Override public void writeToParcel(@NonNull Parcel out, int flags) { super.writeToParcel(out, flags); out.writeStringList(favoriteDessert); } … }
  17. 17. @Override public Parcelable onSaveInstanceState() { Parcelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); ss.favoriteDessert = getFavoriteDesserts(); return ss; } @Override public void onRestoreInstanceState(Parcelable state) { SavedState ss = (SavedState) state; super.onRestoreInstanceState(ss.getSuperState()); setFavoriteDesserts(ss.favoriteDessert); requestLayout(); }
  18. 18. static class SavedState extends BaseSavedState { String imageUrl; String uriString; SavedState(Parcelable superState) { super(superState); } private SavedState(Parcel in) { super(in); imageUrl = in.readString(); uriString = in.readString(); } @Override public void writeToParcel(@NonNull Parcel out, int flags) { super.writeToParcel(out, flags); out.writeString(imageUrl); out.writeString(uriString); } … }
  19. 19. @Override public Parcelable onSaveInstanceState() { Parcelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); ss.imageUrl = imageUrl; if (localImageUri != null) { ss.uriString = localImageUri.toString(); } return ss; } @Override public void onRestoreInstanceState(Parcelable state) { SavedState ss = (SavedState) state; super.onRestoreInstanceState(ss.getSuperState()); if (!TextUtils.isEmpty(ss.uriString)) { setImageFromUriString(ss.uriString); } else if (!TextUtils.isEmpty(ss.imageUrl)) { setImageUrl(ss.imageUrl); } requestLayout(); }
  20. 20. final int id = eventGroup.getCheckedRadioButtonId(); switch (id) { case R.id.attend: profileData.eventStatus = ProfileData.EVENT_STATUS_ATTEN break; case R.id.not_attend: profileData.eventStatus = ProfileData.EVENT_STATUS_NOT_A break; case R.id.unknown: profileData.eventStatus = ProfileData.EVENT_STATUS_UNKNO break; default: return; }
  21. 21. @Retention(RetentionPolicy.SOURCE) @IntDef({EVENT_STATUS_UNDEFINED, EVENT_STATUS_ATTEND, EVENT_STATUS_NOT_ATTEND, EVENT_STATUS_UNKNOWN}) public @interface EventStatus { } @EventStatus public int getEventStatus() { final int id = getCheckedRadioButtonId(); switch (id) { case R.id.attend: return EVENT_STATUS_ATTEND; case R.id.not_attend: return EVENT_STATUS_NOT_ATTEND; case R.id.unknown: return EVENT_STATUS_UNKNOWN; default: return EVENT_STATUS_UNDEFINED; } }
  22. 22. private boolean validate() { // バリデーション失敗: エラーメッセージ表示 final String name = nameEditText.getText().toString(); if (TextUtils.isEmpty(name)) { Toast.makeText(MainActivity.this, "名前が入力されていません", Toast.LENGTH_SHORT).show( return false; } if (eventStatusPicker.getEventStatus() == EventStatusPicker.EVENT_STATUS_UNDEFINED) { Toast.makeText(MainActivity.this, "DroidKaigiが選択されていません", Toast.LENGTH_SHORT return false; } return true; }

×