SlideShare a Scribd company logo
1 of 21
Download to read offline
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/
Problem
•

How communicate between Activity and Fragment?

?

Activity

Fragment
#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!!!

More Related Content

What's hot

iOS Unit Testing
iOS Unit TestingiOS Unit Testing
iOS Unit Testingsgleadow
 
Unit Testing in iOS - Ninjava Talk
Unit Testing in iOS - Ninjava TalkUnit Testing in iOS - Ninjava Talk
Unit Testing in iOS - Ninjava TalkLong Weekend LLC
 
Android Test Pyramid - Ágiles 2013
Android Test Pyramid - Ágiles 2013Android Test Pyramid - Ágiles 2013
Android Test Pyramid - Ágiles 2013Rafael Portela
 
From DevOps to GitOps with GitLab
From DevOps to GitOps with GitLabFrom DevOps to GitOps with GitLab
From DevOps to GitOps with GitLabChen Cheng-Wei
 
Programming katas for Software Testers - CounterStrings
Programming katas for Software Testers - CounterStringsProgramming katas for Software Testers - CounterStrings
Programming katas for Software Testers - CounterStringsAlan Richardson
 
Interpreter RPG to Java
Interpreter RPG to JavaInterpreter RPG to Java
Interpreter RPG to Javafarerobe
 
Unit Testing RPG with JUnit
Unit Testing RPG with JUnitUnit Testing RPG with JUnit
Unit Testing RPG with JUnitGreg.Helton
 

What's hot (8)

iOS Unit Testing
iOS Unit TestingiOS Unit Testing
iOS Unit Testing
 
Unit Testing in iOS - Ninjava Talk
Unit Testing in iOS - Ninjava TalkUnit Testing in iOS - Ninjava Talk
Unit Testing in iOS - Ninjava Talk
 
Android Test Pyramid - Ágiles 2013
Android Test Pyramid - Ágiles 2013Android Test Pyramid - Ágiles 2013
Android Test Pyramid - Ágiles 2013
 
From DevOps to GitOps with GitLab
From DevOps to GitOps with GitLabFrom DevOps to GitOps with GitLab
From DevOps to GitOps with GitLab
 
Unit Testing in iOS
Unit Testing in iOSUnit Testing in iOS
Unit Testing in iOS
 
Programming katas for Software Testers - CounterStrings
Programming katas for Software Testers - CounterStringsProgramming katas for Software Testers - CounterStrings
Programming katas for Software Testers - CounterStrings
 
Interpreter RPG to Java
Interpreter RPG to JavaInterpreter RPG to Java
Interpreter RPG to Java
 
Unit Testing RPG with JUnit
Unit Testing RPG with JUnitUnit Testing RPG with JUnit
Unit Testing RPG with JUnit
 

Viewers also liked

Potato02 ViewSticker作りました
Potato02 ViewSticker作りましたPotato02 ViewSticker作りました
Potato02 ViewSticker作りましたToshihiro Yagi
 
Potato01 No more 手書き ViewHolder
Potato01 No more 手書き ViewHolderPotato01 No more 手書き ViewHolder
Potato01 No more 手書き ViewHolderToshihiro Yagi
 
Potato03 KotlinでAndroidアプリ開発(後編)
Potato03 KotlinでAndroidアプリ開発(後編)Potato03 KotlinでAndroidアプリ開発(後編)
Potato03 KotlinでAndroidアプリ開発(後編)Toshihiro Yagi
 
Re viewのリアルタイムプレギュー機能をつくってみたよ
Re viewのリアルタイムプレギュー機能をつくってみたよRe viewのリアルタイムプレギュー機能をつくってみたよ
Re viewのリアルタイムプレギュー機能をつくってみたよToshihiro Yagi
 
Firefox osでlineは作れるか
Firefox osでlineは作れるかFirefox osでlineは作れるか
Firefox osでlineは作れるかToshihiro Yagi
 

Viewers also liked (8)

Abc2011 2 yagi
Abc2011 2 yagiAbc2011 2 yagi
Abc2011 2 yagi
 
Flight入門
Flight入門Flight入門
Flight入門
 
Abc2011 yagi
Abc2011 yagiAbc2011 yagi
Abc2011 yagi
 
Potato02 ViewSticker作りました
Potato02 ViewSticker作りましたPotato02 ViewSticker作りました
Potato02 ViewSticker作りました
 
Potato01 No more 手書き ViewHolder
Potato01 No more 手書き ViewHolderPotato01 No more 手書き ViewHolder
Potato01 No more 手書き ViewHolder
 
Potato03 KotlinでAndroidアプリ開発(後編)
Potato03 KotlinでAndroidアプリ開発(後編)Potato03 KotlinでAndroidアプリ開発(後編)
Potato03 KotlinでAndroidアプリ開発(後編)
 
Re viewのリアルタイムプレギュー機能をつくってみたよ
Re viewのリアルタイムプレギュー機能をつくってみたよRe viewのリアルタイムプレギュー機能をつくってみたよ
Re viewのリアルタイムプレギュー機能をつくってみたよ
 
Firefox osでlineは作れるか
Firefox osでlineは作れるかFirefox osでlineは作れるか
Firefox osでlineは作れるか
 

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

深入淺出談Fragment
深入淺出談Fragment深入淺出談Fragment
深入淺出談Fragment毅 方
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2Leonid Maslov
 
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testabilityPHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testabilityGiorgio Sironi
 
Droidcon ES '16 - How to fail going offline
Droidcon ES '16 - How to fail going offlineDroidcon ES '16 - How to fail going offline
Droidcon ES '16 - How to fail going offlineJavier de Pedro López
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++ppd1961
 
Встреча Google Post IO ( Владимир Иванов, Катерина Заворотченко и Сергей Комлач)
Встреча Google Post IO ( Владимир Иванов, Катерина Заворотченко и Сергей Комлач)Встреча Google Post IO ( Владимир Иванов, Катерина Заворотченко и Сергей Комлач)
Встреча Google Post IO ( Владимир Иванов, Катерина Заворотченко и Сергей Комлач)Alina Vilk
 
Performance #6 threading
Performance #6  threadingPerformance #6  threading
Performance #6 threadingVitali Pekelis
 
Lecture #4 activities & fragments
Lecture #4  activities & fragmentsLecture #4  activities & fragments
Lecture #4 activities & fragmentsVitali Pekelis
 
Testing of javacript
Testing of javacriptTesting of javacript
Testing of javacriptLei Kang
 
JavaFX8 TestFX - CDI
JavaFX8   TestFX - CDIJavaFX8   TestFX - CDI
JavaFX8 TestFX - CDISven Ruppert
 
Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Gabor Varadi
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy CodeNaresh Jain
 
Dot Net Accenture
Dot Net AccentureDot Net Accenture
Dot Net AccentureSri K
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMMario Fusco
 
How to code to code less
How to code to code lessHow to code to code less
How to code to code lessAnton Novikau
 
Dave Karow, Split. Powering Progressive Delivery With Data
Dave Karow, Split. Powering Progressive Delivery With DataDave Karow, Split. Powering Progressive Delivery With Data
Dave Karow, Split. Powering Progressive Delivery With DataIT Arena
 
Whoops! Where did my architecture go?
Whoops! Where did my architecture go?Whoops! Where did my architecture go?
Whoops! Where did my architecture go?Oliver Gierke
 

Similar to Potato04 The end of confusion of callback between activity and fragment. (20)

深入淺出談Fragment
深入淺出談Fragment深入淺出談Fragment
深入淺出談Fragment
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2
 
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testabilityPHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
 
Android best practices
Android best practicesAndroid best practices
Android best practices
 
Droidcon ES '16 - How to fail going offline
Droidcon ES '16 - How to fail going offlineDroidcon ES '16 - How to fail going offline
Droidcon ES '16 - How to fail going offline
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++
 
Встреча Google Post IO ( Владимир Иванов, Катерина Заворотченко и Сергей Комлач)
Встреча Google Post IO ( Владимир Иванов, Катерина Заворотченко и Сергей Комлач)Встреча Google Post IO ( Владимир Иванов, Катерина Заворотченко и Сергей Комлач)
Встреча Google Post IO ( Владимир Иванов, Катерина Заворотченко и Сергей Комлач)
 
Performance #6 threading
Performance #6  threadingPerformance #6  threading
Performance #6 threading
 
Lecture #4 activities & fragments
Lecture #4  activities & fragmentsLecture #4  activities & fragments
Lecture #4 activities & fragments
 
Testing of javacript
Testing of javacriptTesting of javacript
Testing of javacript
 
Agile mobile
Agile mobileAgile mobile
Agile mobile
 
JavaFX8 TestFX - CDI
JavaFX8   TestFX - CDIJavaFX8   TestFX - CDI
JavaFX8 TestFX - CDI
 
Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
Dot Net Accenture
Dot Net AccentureDot Net Accenture
Dot Net Accenture
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
 
How to code to code less
How to code to code lessHow to code to code less
How to code to code less
 
Dave Karow, Split. Powering Progressive Delivery With Data
Dave Karow, Split. Powering Progressive Delivery With DataDave Karow, Split. Powering Progressive Delivery With Data
Dave Karow, Split. Powering Progressive Delivery With Data
 
Whoops! Where did my architecture go?
Whoops! Where did my architecture go?Whoops! Where did my architecture go?
Whoops! Where did my architecture go?
 
What the fragments
What the fragmentsWhat the fragments
What the fragments
 

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