Square Fragmentやめるってよ

6139fd48daa26e786794598c4a6b546a?s=47 ninjinkun
October 22, 2014

Square Fragmentやめるってよ

『【翻訳】Android Fragmentへの反対声明』の解説
http://ninjinkun.hatenablog.com/entry/2014/10/16/234611

6139fd48daa26e786794598c4a6b546a?s=128

ninjinkun

October 22, 2014
Tweet

Transcript

  1. 4RVBSF
 'SBHNFOU΍ΊΔͬͯΑ OJOKJOLVO

  2. ʲ຋༁ʳ"OESPJE'SBHNFOU΁ͷ
 ൓ର੠໌ w "EWPDBUJOH"HBJOTU"OESPJE'SBHNFOUT w 4RVBSF͕'SBHNFOUΛ΍Ίͨ࿩ w ษڧ͕ͯΒ຋༁ͨ͠ͷͰɺղઆ͠·͢ w !IPUDIFNJ͞Μʹ຋༁νΣοΫͯ͠௖͖·ͨ͠

    IUUQOJOKJOLVOIBUFOBCMPHDPNFOUSZ
  3. 'SBHNFOUͷ໰୊఺ w ϥΠϑαΠΫϧ͕ෳࡶ w σόοά͕೉͍͠ w 7JFXͱϏδωεϩδοΫͷ෼཭͕ͮ͠Β͍ w ࠶ੜ੒͕ࠇຐज़

  4. ϥΠϑαΠΫϧ͕ෳࡶ w "DUJWJUZ͔Βड͚ܧ͍ͩϥΠϑαΠΫϧ w "DUJWJUZͱ࿈ܞ͢ΔҝͷϥΠϑαΠΫϧ w 'SBHNFOU5SBOTBDUJPOͱ࿈ܞ͢ΔͨΊ ͷϥΠϑαΠΫϧ

  5. σόοάͷ೉͠͞ w ελοΫΛ௥͍ͬͯ͘ͱ'SBHNFOU.BOBHFS*NQM ʹͿ͔ͭͬͯࢮ͵

  6. 7JFX$POUSPMMFSͨΓಘΔ͔ w 'SBHNFOU͸7JFXʹؔ͢ΔϩδοΫ΋͍ͩͿ࣋ͬͯ ͍Δ w ϏδωεϩδοΫͱ྆ํࠞࡏͯ͠͠·͏

  7. 'SBHNFOU5SBOTBDUJPO w DPNNJU͕ඇಉظ w ϝΠϯεϨουͷϋϯυϥΩϡʔͷ͋ͱͰ·ͱΊͯ ࣮ߦ͞ΕΔ w ͙͢ʹ݁ՌΛ࢖͍͍ͨͳΒ FYFDVUF1FOEJOH5SBOTBDUJPOT

  8. 'SBHNFOUΛੜ੒͢Δຐ๏ w 'SBHNFOU͸ϦϑϨΫγϣϯΛ࢖ͬͯ࠶ੜ੒͞ΕΔ w ಗ໊ΫϥεͰ'SBHNFOUΛ࡞Δͱɺ0VUFS$MBTTͷ ࢀর͕ͳͯ͘࠶ੜ੒࣌ʹࢮ͵ w +BWBͷϥΠϑαΠΫϧͱ'SBHNFOUͷϥΠϑαΠΫ ϧ͕ϛεϚον DialogFragment

    dialogFragment = new DialogFragment() { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { ... } }; dialogFragment.show(fragmentManager, tag);
  9. ʢิ଍ʣ'SBHNFOUΛੜ੒͢Δຐ๏ w -JTUFOFS΋อ࣋Ͱ͖ͳ͍ w ਌ͱ௨৴͠Α͏ͱࢥͬͨΒɺ਌ʹ-JTUFOFSΛ࣮૷͢ Δඞཁ͕͋Δ w 'SBHNFOU͔Β"DUJWJUZʹίʔϧόοΫ͢Δํ๏ IUUQZBO[NCMPHTQPUKQGSBHNFOUBDUJWJUZIUNM

  10. public class MainFragment extends Fragment { public interface OnOkBtnClickListener {

    public void onOkClicked(); } private OnOkBtnClickListener mListener; public void setOnOkBtnClickListener(OnOkBtnClickListener l) { mListener = l; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Button okBtn = new Button(inflater.getContext()); okBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mListener != null) { mListener.onOkClicked(); } } }); return okBtn; } } ࠶ੜ੒࣌ʹ-JTUFOFS͕ফ͑Δ IUUQZBO[NCMPHTQPUKQGSBHNFOUBDUJWJUZIUNM
  11. public class MainFragment2 extends Fragment { public interface OnOkBtnClickListener {

    public void onOkClicked(); } private OnOkBtnClickListener mListener; @Override public void onAttach(Activity activity) { super.onAttach(activity); if (activity instanceof OnOkBtnClickListener == false) { throw new ClassCastException("activity ͕ OnOkBtnClickListener Λ࣮૷͍ͯ͠·ͤΜ."); } mListener = ((OnOkBtnClickListener) activity); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Button okBtn = new Button(inflater.getContext()); okBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mListener != null) { mListener.onOkClicked(); } } }); return okBtn; } }
  12. 'SBHNFOU͔ΒֶΜͩ͜ͱ w ୯Ұͷ"DUJWJUZ w ը໘ຖʹ"DUJWJUZΛ෼͚Δඞཁ͸ແ͍ w ΞχϝʔγϣϯͱϥΠϑαΠΫϧ͕؆୯ʹͳΔ

  13. 'SBHNFOU͔ΒֶΜͩ͜ͱ w όοΫελοΫ͸"DUJWJUZಛ༗ͷ֓೦Ͱ͸ͳ͍ w "DUJWJUZͷதʹ΋࣮૷Ͱ͖Δ

  14. 'SBHNFOU͔ΒֶΜͩ͜ͱ w ඞཁͳ΋ͷ͸શ෦ॳΊ͔Βἧ͍ͬͯͨ w "DUJWJUZ 7JFX -BZPVU*OqBUPS

  15. ΧελϜϏϡʔ w $POUBJOFSͷಋೖ w ຕͷը໘Λ7JFXͰදݱ͢Δ w 7JFXPS-BZPVUͷαϒΫϥε w 7JFXͱͯ͠ͷ໾ׂ όοΫૢ࡞ͷ؅ཧ

  16. public class DualPaneContainer extends LinearLayout implements Container { private MyDetailView

    detailView; ! public DualPaneContainer(Context context, AttributeSet attrs) { super(context, attrs); } ! @Override protected void onFinishInflate() { super.onFinishInflate(); detailView = (MyDetailView) getChildAt(1); } ! public boolean onBackPressed() { return false; } ! @Override public void showItem(String item) { detailView.setItem(item); } }
  17. public class MainActivity extends Activity { private Container container; !

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); container = (Container) findViewById(R.id.container); } ! public Container getContainer() { return container; } ! @Override public void onBackPressed() { boolean handled = container.onBackPressed(); if (!handled) { finish(); } } }
  18. 7JFXͱ1SFTFOUFS w ϏδωεϩδοΫͱ$POUSPMMFSͷ෼཭ w 1SFTFOUFS w .PEFMΛૢ࡞͢Δ w 7JFXΛૢ࡞͢Δ

  19. ʢิ଍ʣ.71ύλʔϯ w .7$ͱͷҧ͍ w $POUSPMMFS͕ϢʔβʔೖྗΛड͚ͱΒͳ͍ w ड͚ͱΔͷ͸7JFX

  20. public class MyDetailView extends LinearLayout { TextView textView; DetailPresenter presenter;

    ! public MyDetailView(Context context, AttributeSet attrs) { super(context, attrs); presenter = new DetailPresenter(); } ! @Override protected void onFinishInflate() { super.onFinishInflate(); presenter.setView(this); textView = (TextView) findViewById(R.id.text); findViewById(R.id.button).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { presenter.buttonClicked(); } }); } ! public void setItem(String item) { textView.setText(item); } }
  21. class EditDiscountPresenter { // ... public void saveDiscount() { EditDiscountView

    view = getView(); String name = view.getName(); if (isBlank(name)) { view.showNameRequiredWarning(); return; } if (isNewDiscount()) { createNewDiscountAsync(name, view.getAmount(), view.isPercentage()); } else { updateNewDiscountAsync(discountId, name, view.getAmount(), view.isPercentage()); } close(); } }
  22. OJOKJOLVOࢲݟ w "DUJWJUZͷܽؕʢͱ͍͏͔χʔζʹ߹͍ͬͯͳ͔ͬ ͨͱ͜Ζʣ w "DUJWJUZؒͷૄ݁߹ઃܭ w ը໘લఏ

  23. OJOKJOLVOࢲݟ w "DUJWJUZͷܽ఺Λิ׬͢ΔͨΊͷ'SBHNFOU w "DUJWJUZͱͷ੔߹ੑΛऔΖ͏ͱ͗ͯ͢͠ઃܭ͕ෳ ࡶʹͳͬͯ͠·͍ͬͯΔ w 4RVBSF͸੔߹ੑΛࣺͯͯɺಠࣗͷεςʔτϑϧੈ քΛߏங͢ΔΞϓϩʔν

  24. OJOKJOLVOࢲݟ w ϖʔδભҠ͢Δ8FCΞϓϦͱ4JOHMF1BHF "QQMJDBUJPOͷؔ܎ʹࣅͯΔ w ͳͷͰࢀߟʹͳΔ͜ͱ΋͋Δ͔΋

  25. OJOKJOLVOࢲݟ w ฐࣾͰ͸'SBHNFOU͸ࣺͯͳ͍ w ಠࣗͰੈք؍Λ࡞Δ༨༟͸ͳ͍ w ͔͠͠'SBHNFOUͷਏ͞͸ೝ͓ࣝͯ͘͠΂͖ͩͱࢥ ͏

  26. ձ৔σΟεΧογϣϯ w #BDLTUBDLʹ٧Έ͍ͨͱ͖͚ͩ'SBHNFOUΛ࢖͏ύ λʔϯΛࢼ͍ͯ͠Δํ͕͍Βͬ͠Όͬͨ w $VTUPN7JFXΛ΋ͬͱ࢖͏Α͏ʹ͢Ε͹'SBHNFOU ґଘ͸ݮΒͤΔ w 'SBHNFOUʹԿͰ΋ॻ͘ͷ͸ԿʹͤΑΞϯνύλʔ ϯ

    w 1SFTFOUFS͸'SBHNFOUΛ࢖͍ͬͯͯ΋༗༻ͦ͏
  27. ձ৔σΟεΧογϣϯ w Ͱ͖Δ͚ͩ"DUJWJUZ͚ͩͰ࡞Δ೿ͷํ΋ w "DUJWJUZʹ͍ͭͯ͸͋·Γ΢έ͕ྑ͘ͳ͔ͬͨ

  28. ձ৔σΟεΧογϣϯ w 'SBHNFOUͷਏ͞ʹ͍ͭͯ͸݁ߏίϯηϯαε͕औ Εͨͱࢥ͏ w ͦͷลΓΛཧղ্ͨ͠Ͱ࢖͍͖ͬͯ·͠ΐ͏