SlideShare a Scribd company logo
おいしいFragment
           31.3.2012
Takahiro Yoshimura (@alterakey)
自己紹介

こんな人

 埼玉で活動しているアーキテクトです。

 FlashゲームからWebアプリまで。

 Twitterやってます→@alterakey
さて


Fragmentと聞いて

 何を思い浮べますか?

 何に使えるものなのでしょう?
Fragment?

Fragmentとは断片と見つけたり

Activityを機能的に分解する方法

大画面対応だけじゃない!

Android 1.6∼で使えます
大事なことなので


Android 1.6∼で使えます。

Support Package
簡単な例


Activityから他のアプリを呼び出しましょう。

Activity.startActivityForResult()

Activity.onActivityResult()
指針

MainActivity.java(抜粋)

public void onCreate(...) {
    ...
    this.startActivityForResult(intent, REQ_1);
    ...
}
...
public void onActivityResult(int requestCode, ...) {
    if (requestCode == REQ_1) {
        ...
    }
    ...
}
もしもこれが

Activity.startActivityForResult()を複数箇所で
呼び出していたら…?

Activityがもっと複雑だったら…?

カオス!!
指針


見通しを確保するために…

Activityから機能を分離します。

 REQ_1にまつわる部分
指針

MainActivity.java(抜粋)

public class MainActivity extends FragmentActivity {
...
    public void onCreate(...) {
      ...
      getSupportFragmentManager()
          .beginTransaction()
          .add(new RequestOneFragment(), null)
          .commit();
      ...
    }
...
指針

MainActivity.java(抜粋)

...
private class RequestOneFragment extends Fragment {
    private static final int REQ = 1;
    @Override
    public void onCreate(...) {
      super.onCreate(...);
      this.startActivityForResult(intent, REQ);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, ...) {
      ...
    }
}
こうすると

「機能」ごとにまとまってすっきりする

同じようなことをしたい時に使い回しが効く
→機能を抽出しているので当然といえば当然…

状態を保存するのにも使える

しかし自動テストの役には立ちにくい…
大画面対応と聞くけど


ActivityからFragmentとしてUIを分離

条件に合わせてレイアウトを読んで、
Fragmentをはめこんで行けば…
(e.g. 狭ければ別Activity、広ければペイン)
まとめ

FragmentはActivityの持つ「機能」のこと

ペイン型UIやタブレットは関係ない

今からでも使える

しかし自動テストの役にはあまり立たない
まとめ

gdgdになりがちだったActivity周辺の見通し確
保にも有効

再利用もできる

ガンガン使おう!
ご静聴ありがとうございました。

More Related Content

Viewers also liked (14)

Viagra-ranking-report
Viagra-ranking-reportViagra-ranking-report
Viagra-ranking-report
serpIQ
 
伝授の巻について #スクエアフリーセミナー #yochiand
伝授の巻について #スクエアフリーセミナー #yochiand伝授の巻について #スクエアフリーセミナー #yochiand
伝授の巻について #スクエアフリーセミナー #yochiand
Takahiro Yoshimura
 
アプリを弄ってみる #3 #antama_ws
アプリを弄ってみる #3 #antama_wsアプリを弄ってみる #3 #antama_ws
アプリを弄ってみる #3 #antama_ws
Takahiro Yoshimura
 
Strategies for class
Strategies for classStrategies for class
Strategies for class
nucmxh
 
serp-iq-buy-viagra-ranking-report
serp-iq-buy-viagra-ranking-reportserp-iq-buy-viagra-ranking-report
serp-iq-buy-viagra-ranking-report
serpIQ
 
アプリを弄ってみる #1 #antama_ws
アプリを弄ってみる #1 #antama_wsアプリを弄ってみる #1 #antama_ws
アプリを弄ってみる #1 #antama_ws
Takahiro Yoshimura
 
参考書選びと迷った時の勉強法。 #antama_ws
参考書選びと迷った時の勉強法。 #antama_ws参考書選びと迷った時の勉強法。 #antama_ws
参考書選びと迷った時の勉強法。 #antama_ws
Takahiro Yoshimura
 
Eco Shift Power Corp. Overview
Eco Shift Power Corp. OverviewEco Shift Power Corp. Overview
Eco Shift Power Corp. Overview
ryancook003
 
アプリを弄ってみる #2 #antama_ws
アプリを弄ってみる #2 #antama_wsアプリを弄ってみる #2 #antama_ws
アプリを弄ってみる #2 #antama_ws
Takahiro Yoshimura
 
serp-iq-life-insurance-ranking-report
serp-iq-life-insurance-ranking-reportserp-iq-life-insurance-ranking-report
serp-iq-life-insurance-ranking-report
serpIQ
 
CLAS PLUACLD071 Evaluation
CLAS PLUACLD071 EvaluationCLAS PLUACLD071 Evaluation
CLAS PLUACLD071 Evaluation
clasleaders
 
LESION LIGAMENTO CRUZADO ANTERIOR
LESION LIGAMENTO CRUZADO ANTERIORLESION LIGAMENTO CRUZADO ANTERIOR
LESION LIGAMENTO CRUZADO ANTERIOR
Bernell Marquez
 
Fracturas e inmovilizaciones
Fracturas e inmovilizacionesFracturas e inmovilizaciones
Fracturas e inmovilizaciones
Bernell Marquez
 
Enfermedad de charcot marie tooth
Enfermedad de charcot marie toothEnfermedad de charcot marie tooth
Enfermedad de charcot marie tooth
Bernell Marquez
 
Viagra-ranking-report
Viagra-ranking-reportViagra-ranking-report
Viagra-ranking-report
serpIQ
 
伝授の巻について #スクエアフリーセミナー #yochiand
伝授の巻について #スクエアフリーセミナー #yochiand伝授の巻について #スクエアフリーセミナー #yochiand
伝授の巻について #スクエアフリーセミナー #yochiand
Takahiro Yoshimura
 
アプリを弄ってみる #3 #antama_ws
アプリを弄ってみる #3 #antama_wsアプリを弄ってみる #3 #antama_ws
アプリを弄ってみる #3 #antama_ws
Takahiro Yoshimura
 
Strategies for class
Strategies for classStrategies for class
Strategies for class
nucmxh
 
serp-iq-buy-viagra-ranking-report
serp-iq-buy-viagra-ranking-reportserp-iq-buy-viagra-ranking-report
serp-iq-buy-viagra-ranking-report
serpIQ
 
アプリを弄ってみる #1 #antama_ws
アプリを弄ってみる #1 #antama_wsアプリを弄ってみる #1 #antama_ws
アプリを弄ってみる #1 #antama_ws
Takahiro Yoshimura
 
参考書選びと迷った時の勉強法。 #antama_ws
参考書選びと迷った時の勉強法。 #antama_ws参考書選びと迷った時の勉強法。 #antama_ws
参考書選びと迷った時の勉強法。 #antama_ws
Takahiro Yoshimura
 
Eco Shift Power Corp. Overview
Eco Shift Power Corp. OverviewEco Shift Power Corp. Overview
Eco Shift Power Corp. Overview
ryancook003
 
アプリを弄ってみる #2 #antama_ws
アプリを弄ってみる #2 #antama_wsアプリを弄ってみる #2 #antama_ws
アプリを弄ってみる #2 #antama_ws
Takahiro Yoshimura
 
serp-iq-life-insurance-ranking-report
serp-iq-life-insurance-ranking-reportserp-iq-life-insurance-ranking-report
serp-iq-life-insurance-ranking-report
serpIQ
 
CLAS PLUACLD071 Evaluation
CLAS PLUACLD071 EvaluationCLAS PLUACLD071 Evaluation
CLAS PLUACLD071 Evaluation
clasleaders
 
LESION LIGAMENTO CRUZADO ANTERIOR
LESION LIGAMENTO CRUZADO ANTERIORLESION LIGAMENTO CRUZADO ANTERIOR
LESION LIGAMENTO CRUZADO ANTERIOR
Bernell Marquez
 
Fracturas e inmovilizaciones
Fracturas e inmovilizacionesFracturas e inmovilizaciones
Fracturas e inmovilizaciones
Bernell Marquez
 
Enfermedad de charcot marie tooth
Enfermedad de charcot marie toothEnfermedad de charcot marie tooth
Enfermedad de charcot marie tooth
Bernell Marquez
 

Similar to おいしいFragment #antama_ws (20)

Tokyo GTUG Bootcamp2010
Tokyo GTUG Bootcamp2010Tokyo GTUG Bootcamp2010
Tokyo GTUG Bootcamp2010
Takashi EGAWA
 
GroovyなAndroidテスト #atest_hack
GroovyなAndroidテスト #atest_hackGroovyなAndroidテスト #atest_hack
GroovyなAndroidテスト #atest_hack
Takahiro Yoshimura
 
GDG DevFest 2020 Android data linkage info
GDG DevFest 2020 Android data linkage infoGDG DevFest 2020 Android data linkage info
GDG DevFest 2020 Android data linkage info
tsutomuhayakawa
 
ちょっと詳しくJavaScript 特別編【悪霊の神々】
ちょっと詳しくJavaScript 特別編【悪霊の神々】ちょっと詳しくJavaScript 特別編【悪霊の神々】
ちょっと詳しくJavaScript 特別編【悪霊の神々】
株式会社ランチェスター
 
AsyncTask アンチパターン
AsyncTask アンチパターンAsyncTask アンチパターン
AsyncTask アンチパターン
Hiroshi Kurokawa
 
goog.ui.Component のはぐれかた
goog.ui.Component のはぐれかたgoog.ui.Component のはぐれかた
goog.ui.Component のはぐれかた
Soichi Takamura
 
Using the Fragments(Android)
Using the Fragments(Android)Using the Fragments(Android)
Using the Fragments(Android)
Teruaki Kinoshita
 
Build your AR app by using AR Foundation samples
Build your AR app by using AR Foundation samplesBuild your AR app by using AR Foundation samples
Build your AR app by using AR Foundation samples
Hirokazu Egashira
 
Node.js - JavaScript Thread Programming
Node.js - JavaScript Thread ProgrammingNode.js - JavaScript Thread Programming
Node.js - JavaScript Thread Programming
takesako
 
Android4.2徹底解剖!
Android4.2徹底解剖!Android4.2徹底解剖!
Android4.2徹底解剖!
leverages_event
 
OnActivityResult - おまえら!もうonActivityResultでswitchとif書く時代は終わりだぞ!
OnActivityResult - おまえら!もうonActivityResultでswitchとif書く時代は終わりだぞ!OnActivityResult - おまえら!もうonActivityResultでswitchとif書く時代は終わりだぞ!
OnActivityResult - おまえら!もうonActivityResultでswitchとif書く時代は終わりだぞ!
Shinobu Okano
 
Twitter sphere of #twitter4j #twtr_hack
Twitter sphere of #twitter4j #twtr_hackTwitter sphere of #twitter4j #twtr_hack
Twitter sphere of #twitter4j #twtr_hack
kimukou_26 Kimukou
 
第三回ありえる社内勉強会 「いわががのLombok」
第三回ありえる社内勉強会 「いわががのLombok」第三回ありえる社内勉強会 「いわががのLombok」
第三回ありえる社内勉強会 「いわががのLombok」
yoshiaki iwanaga
 
Androidの通信周りのコーディングについて
Androidの通信周りのコーディングについてAndroidの通信周りのコーディングについて
Androidの通信周りのコーディングについて
Shoichi Takagi
 
Ecmascript2015とその周辺について
Ecmascript2015とその周辺についてEcmascript2015とその周辺について
Ecmascript2015とその周辺について
豊明 尾古
 
Unit testing JavaScript with JUnit/JavaFX
Unit testing JavaScript with JUnit/JavaFXUnit testing JavaScript with JUnit/JavaFX
Unit testing JavaScript with JUnit/JavaFX
Shinya Mochida
 
Android Lecture #03 @PRO&BSC Inc.
Android Lecture #03 @PRO&BSC Inc.Android Lecture #03 @PRO&BSC Inc.
Android Lecture #03 @PRO&BSC Inc.
Yuki Higuchi
 
Rx java x retrofit
Rx java x retrofitRx java x retrofit
Rx java x retrofit
Shun Nakahara
 
JavaScript 実践講座 Framework, Tool, Performance
JavaScript 実践講座 Framework, Tool, PerformanceJavaScript 実践講座 Framework, Tool, Performance
JavaScript 実践講座 Framework, Tool, Performance
クラスメソッド株式会社
 
Tokyo GTUG Bootcamp2010
Tokyo GTUG Bootcamp2010Tokyo GTUG Bootcamp2010
Tokyo GTUG Bootcamp2010
Takashi EGAWA
 
GroovyなAndroidテスト #atest_hack
GroovyなAndroidテスト #atest_hackGroovyなAndroidテスト #atest_hack
GroovyなAndroidテスト #atest_hack
Takahiro Yoshimura
 
GDG DevFest 2020 Android data linkage info
GDG DevFest 2020 Android data linkage infoGDG DevFest 2020 Android data linkage info
GDG DevFest 2020 Android data linkage info
tsutomuhayakawa
 
ちょっと詳しくJavaScript 特別編【悪霊の神々】
ちょっと詳しくJavaScript 特別編【悪霊の神々】ちょっと詳しくJavaScript 特別編【悪霊の神々】
ちょっと詳しくJavaScript 特別編【悪霊の神々】
株式会社ランチェスター
 
AsyncTask アンチパターン
AsyncTask アンチパターンAsyncTask アンチパターン
AsyncTask アンチパターン
Hiroshi Kurokawa
 
goog.ui.Component のはぐれかた
goog.ui.Component のはぐれかたgoog.ui.Component のはぐれかた
goog.ui.Component のはぐれかた
Soichi Takamura
 
Using the Fragments(Android)
Using the Fragments(Android)Using the Fragments(Android)
Using the Fragments(Android)
Teruaki Kinoshita
 
Build your AR app by using AR Foundation samples
Build your AR app by using AR Foundation samplesBuild your AR app by using AR Foundation samples
Build your AR app by using AR Foundation samples
Hirokazu Egashira
 
Node.js - JavaScript Thread Programming
Node.js - JavaScript Thread ProgrammingNode.js - JavaScript Thread Programming
Node.js - JavaScript Thread Programming
takesako
 
Android4.2徹底解剖!
Android4.2徹底解剖!Android4.2徹底解剖!
Android4.2徹底解剖!
leverages_event
 
OnActivityResult - おまえら!もうonActivityResultでswitchとif書く時代は終わりだぞ!
OnActivityResult - おまえら!もうonActivityResultでswitchとif書く時代は終わりだぞ!OnActivityResult - おまえら!もうonActivityResultでswitchとif書く時代は終わりだぞ!
OnActivityResult - おまえら!もうonActivityResultでswitchとif書く時代は終わりだぞ!
Shinobu Okano
 
Twitter sphere of #twitter4j #twtr_hack
Twitter sphere of #twitter4j #twtr_hackTwitter sphere of #twitter4j #twtr_hack
Twitter sphere of #twitter4j #twtr_hack
kimukou_26 Kimukou
 
第三回ありえる社内勉強会 「いわががのLombok」
第三回ありえる社内勉強会 「いわががのLombok」第三回ありえる社内勉強会 「いわががのLombok」
第三回ありえる社内勉強会 「いわががのLombok」
yoshiaki iwanaga
 
Androidの通信周りのコーディングについて
Androidの通信周りのコーディングについてAndroidの通信周りのコーディングについて
Androidの通信周りのコーディングについて
Shoichi Takagi
 
Ecmascript2015とその周辺について
Ecmascript2015とその周辺についてEcmascript2015とその周辺について
Ecmascript2015とその周辺について
豊明 尾古
 
Unit testing JavaScript with JUnit/JavaFX
Unit testing JavaScript with JUnit/JavaFXUnit testing JavaScript with JUnit/JavaFX
Unit testing JavaScript with JUnit/JavaFX
Shinya Mochida
 
Android Lecture #03 @PRO&BSC Inc.
Android Lecture #03 @PRO&BSC Inc.Android Lecture #03 @PRO&BSC Inc.
Android Lecture #03 @PRO&BSC Inc.
Yuki Higuchi
 
Ad

More from Takahiro Yoshimura (20)

Looking Back 2023
Looking Back 2023Looking Back 2023
Looking Back 2023
Takahiro Yoshimura
 
Fill In The Blank
Fill In The BlankFill In The Blank
Fill In The Blank
Takahiro Yoshimura
 
Ticket To The Dark World
Ticket To The Dark WorldTicket To The Dark World
Ticket To The Dark World
Takahiro Yoshimura
 
Looking back 2022
Looking back 2022Looking back 2022
Looking back 2022
Takahiro Yoshimura
 
Hazy Datagrams
Hazy DatagramsHazy Datagrams
Hazy Datagrams
Takahiro Yoshimura
 
Wartime Pigeons
Wartime PigeonsWartime Pigeons
Wartime Pigeons
Takahiro Yoshimura
 
Securing Supply Chains
Securing Supply ChainsSecuring Supply Chains
Securing Supply Chains
Takahiro Yoshimura
 
Looking Back: CIS on Managed K8S
Looking Back: CIS on Managed K8SLooking Back: CIS on Managed K8S
Looking Back: CIS on Managed K8S
Takahiro Yoshimura
 
Beijing Report 2022
Beijing Report 2022Beijing Report 2022
Beijing Report 2022
Takahiro Yoshimura
 
Asynchronicity
AsynchronicityAsynchronicity
Asynchronicity
Takahiro Yoshimura
 
Outsmarting Smartphone Apps 2
Outsmarting Smartphone Apps 2Outsmarting Smartphone Apps 2
Outsmarting Smartphone Apps 2
Takahiro Yoshimura
 
Outsmarting Smartphone Apps 2
Outsmarting Smartphone Apps 2Outsmarting Smartphone Apps 2
Outsmarting Smartphone Apps 2
Takahiro Yoshimura
 
Outsmarting Smartphone Apps
Outsmarting Smartphone AppsOutsmarting Smartphone Apps
Outsmarting Smartphone Apps
Takahiro Yoshimura
 
Drawing Dataflow On Dalvik Bytecodes
Drawing Dataflow On Dalvik Bytecodes Drawing Dataflow On Dalvik Bytecodes
Drawing Dataflow On Dalvik Bytecodes
Takahiro Yoshimura
 
Trueseeing: Effective Dataflow Analysis over Dalvik Opcodes
Trueseeing: Effective Dataflow Analysis over Dalvik OpcodesTrueseeing: Effective Dataflow Analysis over Dalvik Opcodes
Trueseeing: Effective Dataflow Analysis over Dalvik Opcodes
Takahiro Yoshimura
 
10 (about make 10 with 4 numbers challenge)
10 (about make 10 with 4 numbers challenge)10 (about make 10 with 4 numbers challenge)
10 (about make 10 with 4 numbers challenge)
Takahiro Yoshimura
 
Go goes Mobile: Quick Exploration on Go 1.5 and Gomobile
Go goes Mobile: Quick Exploration on Go 1.5 and GomobileGo goes Mobile: Quick Exploration on Go 1.5 and Gomobile
Go goes Mobile: Quick Exploration on Go 1.5 and Gomobile
Takahiro Yoshimura
 
Android Wear: Good Parts
Android Wear: Good PartsAndroid Wear: Good Parts
Android Wear: Good Parts
Takahiro Yoshimura
 
DEFCON21×S2 REPORT
DEFCON21×S2 REPORTDEFCON21×S2 REPORT
DEFCON21×S2 REPORT
Takahiro Yoshimura
 
あげフォト開発ノート #antama_ws
あげフォト開発ノート #antama_wsあげフォト開発ノート #antama_ws
あげフォト開発ノート #antama_ws
Takahiro Yoshimura
 
Looking Back: CIS on Managed K8S
Looking Back: CIS on Managed K8SLooking Back: CIS on Managed K8S
Looking Back: CIS on Managed K8S
Takahiro Yoshimura
 
Drawing Dataflow On Dalvik Bytecodes
Drawing Dataflow On Dalvik Bytecodes Drawing Dataflow On Dalvik Bytecodes
Drawing Dataflow On Dalvik Bytecodes
Takahiro Yoshimura
 
Trueseeing: Effective Dataflow Analysis over Dalvik Opcodes
Trueseeing: Effective Dataflow Analysis over Dalvik OpcodesTrueseeing: Effective Dataflow Analysis over Dalvik Opcodes
Trueseeing: Effective Dataflow Analysis over Dalvik Opcodes
Takahiro Yoshimura
 
10 (about make 10 with 4 numbers challenge)
10 (about make 10 with 4 numbers challenge)10 (about make 10 with 4 numbers challenge)
10 (about make 10 with 4 numbers challenge)
Takahiro Yoshimura
 
Go goes Mobile: Quick Exploration on Go 1.5 and Gomobile
Go goes Mobile: Quick Exploration on Go 1.5 and GomobileGo goes Mobile: Quick Exploration on Go 1.5 and Gomobile
Go goes Mobile: Quick Exploration on Go 1.5 and Gomobile
Takahiro Yoshimura
 
あげフォト開発ノート #antama_ws
あげフォト開発ノート #antama_wsあげフォト開発ノート #antama_ws
あげフォト開発ノート #antama_ws
Takahiro Yoshimura
 
Ad

おいしいFragment #antama_ws

Editor's Notes