Potato04 The end of confusion of callback between activity and fragment.
Upcoming SlideShare
Loading in...5
×
 

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

on

  • 1,538 views

The end of confusion of callback between activity and fragment.

The end of confusion of callback between activity and fragment.

Statistics

Views

Total Views
1,538
Views on SlideShare
1,010
Embed Views
528

Actions

Likes
5
Downloads
7
Comments
0

6 Embeds 528

http://sys1yagi.com 487
https://twitter.com 32
http://localhost 4
http://feedly.com 2
http://www.feedspot.com 2
http://digg.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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

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