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

21
The end of confusion of callback between activity and fragment. 2014-02-12 potatotips #4 Toshihiro Yagi

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

Page 1: Potato04 The end of confusion of callback between activity and fragment.

The end of confusion of callback between activity and fragment.

2014-02-12 potatotips #4 Toshihiro Yagi

Page 2: Potato04 The end of confusion of callback between activity and fragment.

About meTwitter : @sys1yagi Blog : http://sys1yagi.com !!Android Engineer at

Page 3: Potato04 The end of confusion of callback between activity and fragment.

出てます。

http://www.amazon.co.jp/gp/product/4798040029/

Page 4: Potato04 The end of confusion of callback between activity and fragment.

Problem• How communicate between Activity and Fragment?

Activity Fragment

?

Page 5: Potato04 The end of confusion of callback between activity and fragment.

@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.

いいんだけどダサくね?

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.

#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.

でも結局Activity破棄-再生成で死ぬよね?

Page 9: Potato04 The end of confusion of callback between activity and fragment.

@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.

濫用するときつい コンテキスト遠い

Page 11: Potato04 The end of confusion of callback between activity and fragment.

#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.

OK but, exaggerated

Page 13: Potato04 The end of confusion of callback between activity and fragment.

In the first place…• なんでActivity-Fragment間連携の為に色々な罠をかいくぐらなければならないんだ

• ついでにDialogFragment-Fragment間もだるい

• 再生成を別々にするのがそもそもおかしくね?

• 基本的にActivity←Fragmentの一方向依存だよね

• 統一的で簡単で罠のない書き方がほしい

Page 14: Potato04 The end of confusion of callback between activity and fragment.

無かったら作る

Page 15: Potato04 The end of confusion of callback between activity and fragment.

indirect injector

Page 16: Potato04 The end of confusion of callback between activity and fragment.

SimplifyIndirect Injector

add dependency

inject request

Declare Interface

Page 17: Potato04 The end of confusion of callback between activity and fragment.

@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.

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.

Enjoy!

Page 20: Potato04 The end of confusion of callback between activity and fragment.

おわりに

https://github.com/sys1yagi/indirect-injector

Welcome your pull request!

GitHub活動を通して個人のキャリアを積みつつ仕事の成果を出したい

Page 21: Potato04 The end of confusion of callback between activity and fragment.

We are hiring!!!