Potato04 The end of confusion of callback between activity and fragment.

2,053 views

Published on

The end of confusion of callback between activity and fragment.

0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,053
On SlideShare
0
From Embeds
0
Number of Embeds
691
Actions
Shares
0
Downloads
11
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Potato04 The end of confusion of callback between activity and fragment.

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

×