potato04 the end of confusion of callback between activity and fragment
TRANSCRIPT
![Page 1: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/1.jpg)
The end of confusion of callback between activity and fragment.
2014-02-12 potatotips #4 Toshihiro Yagi
![Page 3: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/3.jpg)
出てます。
http://www.amazon.co.jp/gp/product/4798040029/
![Page 4: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/4.jpg)
Problem• How communicate between Activity and Fragment?
Activity Fragment
?
![Page 5: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/5.jpg)
@Override public void onAttach(Activity activity) { super.onAttach(activity); try { mCallback = (Listener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement Listener"); } }
#01 Implement a interface in a Activity
Communicating with Other Fragments http://developer.android.com/training/basics/fragments/communicating.html
Declare InterfaceImplements Interface
![Page 6: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/6.jpg)
いいんだけどダサくね?
ClassCastException
mCallback = (Listener) activity;
try{}catch{}
throw new
extends Activity implements Listener{
![Page 7: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/7.jpg)
#02 No rotation or receive config changes.
Set Interface
Declare InterfaceNo rotation
![Page 8: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/8.jpg)
でも結局Activity破棄-再生成で死ぬよね?
![Page 9: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/9.jpg)
@Override protected void onCreate(Bundle save) { super.onCreate(savedInstanceState); bus.register(this); } ! @Subscribe private void onClick(OnClickEvent event){ //do something }
bus.post(new OnClickEvent(10));
#03 Using message bus.
Otto
Otto http://square.github.io/otto/
Message Bus
![Page 10: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/10.jpg)
濫用するときつい コンテキスト遠い
![Page 11: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/11.jpg)
#04 Using Roboguice or Dagger, etc…
Injector Declare Interface
Module
Depend on
![Page 12: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/12.jpg)
OK but, exaggerated
![Page 13: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/13.jpg)
In the first place…• なんでActivity-Fragment間連携の為に色々な罠をかいくぐらなければならないんだ
• ついでにDialogFragment-Fragment間もだるい
• 再生成を別々にするのがそもそもおかしくね?
• 基本的にActivity←Fragmentの一方向依存だよね
• 統一的で簡単で罠のない書き方がほしい
![Page 14: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/14.jpg)
無かったら作る
![Page 15: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/15.jpg)
indirect injector
![Page 16: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/16.jpg)
SimplifyIndirect Injector
add dependency
inject request
Declare Interface
![Page 17: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/17.jpg)
@Override protected void onCreate(Bundle save) { super.onCreate(savedInstanceState); IndirectInjector.addDependency(this, mListener); }
@Inject Listener mListener; !@Override public void onActivityCreated(Bundle savedInstanceState) { IndirectInjector.inject(getActivity(), this); }
SimplifyIndirect Injector
add dependency
inject request
Declare Interface
![Page 18: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/18.jpg)
Good bye confusion• build.gradleにdependencies書いて
• Fragmentの必要なフィールドに@Injectつけて
• Activity側でIndirectInjectorにaddDependency()する
• FragmentのonActivityCreated()でIndirectInjectorのinject()を呼ぶ
• どういう再生成が走っても大丈夫
![Page 19: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/19.jpg)
Enjoy!
![Page 20: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/20.jpg)
おわりに
https://github.com/sys1yagi/indirect-injector
Welcome your pull request!
GitHub活動を通して個人のキャリアを積みつつ仕事の成果を出したい
![Page 21: Potato04 The end of confusion of callback between activity and fragment](https://reader030.vdocuments.pub/reader030/viewer/2022020218/55a2d7c61a28ab9c7d8b45ac/html5/thumbnails/21.jpg)
We are hiring!!!