SlideShare una empresa de Scribd logo
1 de 14
In 2016, how do you implement
"Web API client Activity"?
Tsuyoshi Yoshioka (@tsuyogoro)
Mercari, Inc.
About me
Tsuyoshi Yoshioka (@tsuyogoro)
Android engineer
Mercari, Inc
How do you implement this app?
Using a web API - http://zipcloud.ibsnet.co.jp/doc/api
Today’s focus on
How to support “orientation change” ?
Base of my idea
Activity
(Portrait)
Activity
(Landscape)
Data
Model
API task
Web API
(Search address)
share share
“Wait…” dialog
1. How to share “Data model” ?
2. How to share “API task” ?
3. How to show ProgressDialog in correct period ?
We have 3 weapons
● Databinding
● RxJava
● Lifecycle of Activity & Fragment
1.Sharing model with databinding
public class ZipSearchActivityModel implements Serializable {
private static final long serialVersionUID = 123L;
/**
* ユーザの入力した郵便番号
*
*/
public ObservableField<String> zipCode
= new ObservableField<>();
/**
* 検索した結果の住所
* (場合によってはエラー文言を表示するのに使う)
*
*/
public ObservableField<String> address
= new ObservableField<>();
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={model.zipCode}"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:text="@{model.address}"/>
☆ Two-way binding
https://halfthought.wordpress.com/2016/03/23/2-way-data-binding-on-android/
1.Sharing model with databinding
Can implement model sharing,
Quite simple & safely
public class ZipSearchActivityModel implements Serializable {
private static final long serialVersionUID = 123L;
/**
* ユーザの入力した郵便番号
*
*/
public ObservableField<String> zipCode
= new ObservableField<>();
/**
* 検索した結果の住所
* (場合によってはエラー文言を表示するのに使う)
*
*/
public ObservableField<String> address
= new ObservableField<>();
} Serializable
Serializable
Can use android studio plug-in
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(BUNDLE_KEY_MODEL, model);
}
2. Task sharing with RxJava
ZipSearchTask.buildRequest(model.zipCode.get())
.flatMap(ZipSearchTask::submitRequest)
.flatMap(ZipSearchTask::parseResponse)
.delay(5, TimeUnit.SECONDS)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(mySubscriber);
Task
Listener
Base of my idea
Activity
(Portrait)
Activity
(Landscape)
Data
Model
Observable
Web API
(Search address)
share shareSubscriber
2. Task sharing with RxJava
public class RxZipSearchActivity extends AppCompatActivity {
private MySubscriber mySubscriber;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mySubscriber = (MySubscriber) getLastCustomNonConfigurationInstance();
if (mySubscriber != null) {
mySubscriber.activeActivity = this;
}
...
@Override
public Object onRetainCustomNonConfigurationInstance() {
return mySubscriber;
}
Can implement task sharing,
Quite simple & safely
3. Progress dialog
private void showProgressDialog() {
if (getSupportFragmentManager().findFragmentByTag(TAG_PROGRESS_DIALOG) == null) {
new ProgressDialogFragment().show(getSupportFragmentManager(), TAG_PROGRESS_DIALOG);
}
}
public static class ProgressDialogFragment extends AppCompatDialogFragment {
@Override
protected void onResume() {
super.onResume();
// まだ通信の途中だったらdialogを出す
if (mySubscriber != null && !mySubscriber.isUnsubscribed()) {
showProgressDialog();
}
● Avoid multiple dialog showing
● Subscriber can be a clue if to show progress dialog
Summary
Introduced my idea to implement simple WebAPI client app
We have efficient way in 2016 (databinding, RxJava)
Let’s discuss how you design / implement such application now?
(Source code intoduced in my presentation)
https://github.com/tsuyoyo/roppongi-aar3-address-search-app
2. RxJava Can implement task sharing, Quite simple & safely
public class RxZipSearchActivity extends AppCompatActivity {
private static class MySubscriber extends Subscriber<List<String>> {
private RxZipSearchActivity activeActivity;
@Override
public void onCompleted() { Log.d("TestTestTest", "onCompleted"); }
@Override
public void onError(Throwable e) { notifyResult(e.getMessage()); }
@Override
public void onNext(List<String> result) {
activeActivity.notifyResult(result);
onCompleted();

Más contenido relacionado

Similar a 160714 roppongi aar3_how_to_implement_activity

Relevance trilogy may dream be with you! (dec17)
Relevance trilogy  may dream be with you! (dec17)Relevance trilogy  may dream be with you! (dec17)
Relevance trilogy may dream be with you! (dec17)Woonsan Ko
 
From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)Jose Manuel Pereira Garcia
 
Designing and Implementing a Multiuser Apps Platform
Designing and Implementing a Multiuser Apps PlatformDesigning and Implementing a Multiuser Apps Platform
Designing and Implementing a Multiuser Apps PlatformApigee | Google Cloud
 
Deep dive into Android async operations
Deep dive into Android async operationsDeep dive into Android async operations
Deep dive into Android async operationsMateusz Grzechociński
 
Spring Cloud Function & Project riff #jsug
Spring Cloud Function & Project riff #jsugSpring Cloud Function & Project riff #jsug
Spring Cloud Function & Project riff #jsugToshiaki Maki
 
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Switzerl...
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Switzerl...Microservices for the Masses with Spring Boot, JHipster, and OAuth - Switzerl...
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Switzerl...Matt Raible
 
Knowing is Understanding: A road trip through Google analytics for Windows Ph...
Knowing is Understanding: A road trip through Google analytics for Windows Ph...Knowing is Understanding: A road trip through Google analytics for Windows Ph...
Knowing is Understanding: A road trip through Google analytics for Windows Ph...blugri software + services BVBA
 
Saindo da zona de conforto… resolvi aprender android
Saindo da zona de conforto… resolvi aprender androidSaindo da zona de conforto… resolvi aprender android
Saindo da zona de conforto… resolvi aprender androidDaniel Baccin
 
droidQuery: The Android port of jQuery
droidQuery: The Android port of jQuerydroidQuery: The Android port of jQuery
droidQuery: The Android port of jQueryPhDBrown
 
Firestore MENA digital days : GDG Abu dhabi
Firestore MENA digital days : GDG Abu dhabiFirestore MENA digital days : GDG Abu dhabi
Firestore MENA digital days : GDG Abu dhabiShashank Kakroo
 
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Jforum S...
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Jforum S...Microservices for the Masses with Spring Boot, JHipster, and OAuth - Jforum S...
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Jforum S...Matt Raible
 
Testing Android apps based on Dagger and RxJava
Testing Android apps based on Dagger and RxJavaTesting Android apps based on Dagger and RxJava
Testing Android apps based on Dagger and RxJavaFabio Collini
 
Skroutz Android MVP and Adapter Delegates presentation
Skroutz Android MVP and Adapter Delegates  presentationSkroutz Android MVP and Adapter Delegates  presentation
Skroutz Android MVP and Adapter Delegates presentationgmetal
 
PredictionIO - Scalable Machine Learning Architecture
PredictionIO - Scalable Machine Learning ArchitecturePredictionIO - Scalable Machine Learning Architecture
PredictionIO - Scalable Machine Learning Architecturepredictionio
 
Gits class #22: [ONLINE] Analyze Your User's Activities Using BigQuery and Da...
Gits class #22: [ONLINE] Analyze Your User's Activities Using BigQuery and Da...Gits class #22: [ONLINE] Analyze Your User's Activities Using BigQuery and Da...
Gits class #22: [ONLINE] Analyze Your User's Activities Using BigQuery and Da...GITS Indonesia
 
Java Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsJava Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsAleksandar Ilić
 
Java Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsJava Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsPSTechSerbia
 

Similar a 160714 roppongi aar3_how_to_implement_activity (20)

Relevance trilogy may dream be with you! (dec17)
Relevance trilogy  may dream be with you! (dec17)Relevance trilogy  may dream be with you! (dec17)
Relevance trilogy may dream be with you! (dec17)
 
Android best practices
Android best practicesAndroid best practices
Android best practices
 
From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)
 
Designing and Implementing a Multiuser Apps Platform
Designing and Implementing a Multiuser Apps PlatformDesigning and Implementing a Multiuser Apps Platform
Designing and Implementing a Multiuser Apps Platform
 
Deep dive into Android async operations
Deep dive into Android async operationsDeep dive into Android async operations
Deep dive into Android async operations
 
Spring Cloud Function & Project riff #jsug
Spring Cloud Function & Project riff #jsugSpring Cloud Function & Project riff #jsug
Spring Cloud Function & Project riff #jsug
 
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Switzerl...
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Switzerl...Microservices for the Masses with Spring Boot, JHipster, and OAuth - Switzerl...
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Switzerl...
 
Knowing is Understanding: A road trip through Google analytics for Windows Ph...
Knowing is Understanding: A road trip through Google analytics for Windows Ph...Knowing is Understanding: A road trip through Google analytics for Windows Ph...
Knowing is Understanding: A road trip through Google analytics for Windows Ph...
 
Saindo da zona de conforto… resolvi aprender android
Saindo da zona de conforto… resolvi aprender androidSaindo da zona de conforto… resolvi aprender android
Saindo da zona de conforto… resolvi aprender android
 
droidQuery: The Android port of jQuery
droidQuery: The Android port of jQuerydroidQuery: The Android port of jQuery
droidQuery: The Android port of jQuery
 
Firestore MENA digital days : GDG Abu dhabi
Firestore MENA digital days : GDG Abu dhabiFirestore MENA digital days : GDG Abu dhabi
Firestore MENA digital days : GDG Abu dhabi
 
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Jforum S...
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Jforum S...Microservices for the Masses with Spring Boot, JHipster, and OAuth - Jforum S...
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Jforum S...
 
Testing Android apps based on Dagger and RxJava
Testing Android apps based on Dagger and RxJavaTesting Android apps based on Dagger and RxJava
Testing Android apps based on Dagger and RxJava
 
Android 3
Android 3Android 3
Android 3
 
Architecture and RxJava
Architecture and RxJavaArchitecture and RxJava
Architecture and RxJava
 
Skroutz Android MVP and Adapter Delegates presentation
Skroutz Android MVP and Adapter Delegates  presentationSkroutz Android MVP and Adapter Delegates  presentation
Skroutz Android MVP and Adapter Delegates presentation
 
PredictionIO - Scalable Machine Learning Architecture
PredictionIO - Scalable Machine Learning ArchitecturePredictionIO - Scalable Machine Learning Architecture
PredictionIO - Scalable Machine Learning Architecture
 
Gits class #22: [ONLINE] Analyze Your User's Activities Using BigQuery and Da...
Gits class #22: [ONLINE] Analyze Your User's Activities Using BigQuery and Da...Gits class #22: [ONLINE] Analyze Your User's Activities Using BigQuery and Da...
Gits class #22: [ONLINE] Analyze Your User's Activities Using BigQuery and Da...
 
Java Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsJava Svet - Communication Between Android App Components
Java Svet - Communication Between Android App Components
 
Java Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsJava Svet - Communication Between Android App Components
Java Svet - Communication Between Android App Components
 

Último

The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 

Último (20)

The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 

160714 roppongi aar3_how_to_implement_activity

  • 1. In 2016, how do you implement "Web API client Activity"? Tsuyoshi Yoshioka (@tsuyogoro) Mercari, Inc.
  • 2. About me Tsuyoshi Yoshioka (@tsuyogoro) Android engineer Mercari, Inc
  • 3. How do you implement this app? Using a web API - http://zipcloud.ibsnet.co.jp/doc/api
  • 4. Today’s focus on How to support “orientation change” ?
  • 5. Base of my idea Activity (Portrait) Activity (Landscape) Data Model API task Web API (Search address) share share “Wait…” dialog 1. How to share “Data model” ? 2. How to share “API task” ? 3. How to show ProgressDialog in correct period ?
  • 6. We have 3 weapons ● Databinding ● RxJava ● Lifecycle of Activity & Fragment
  • 7. 1.Sharing model with databinding public class ZipSearchActivityModel implements Serializable { private static final long serialVersionUID = 123L; /** * ユーザの入力した郵便番号 * */ public ObservableField<String> zipCode = new ObservableField<>(); /** * 検索した結果の住所 * (場合によってはエラー文言を表示するのに使う) * */ public ObservableField<String> address = new ObservableField<>(); <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@={model.zipCode}"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginBottom="16dp" android:text="@{model.address}"/> ☆ Two-way binding https://halfthought.wordpress.com/2016/03/23/2-way-data-binding-on-android/
  • 8. 1.Sharing model with databinding Can implement model sharing, Quite simple & safely public class ZipSearchActivityModel implements Serializable { private static final long serialVersionUID = 123L; /** * ユーザの入力した郵便番号 * */ public ObservableField<String> zipCode = new ObservableField<>(); /** * 検索した結果の住所 * (場合によってはエラー文言を表示するのに使う) * */ public ObservableField<String> address = new ObservableField<>(); } Serializable Serializable Can use android studio plug-in @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putSerializable(BUNDLE_KEY_MODEL, model); }
  • 9. 2. Task sharing with RxJava ZipSearchTask.buildRequest(model.zipCode.get()) .flatMap(ZipSearchTask::submitRequest) .flatMap(ZipSearchTask::parseResponse) .delay(5, TimeUnit.SECONDS) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(mySubscriber); Task Listener
  • 10. Base of my idea Activity (Portrait) Activity (Landscape) Data Model Observable Web API (Search address) share shareSubscriber
  • 11. 2. Task sharing with RxJava public class RxZipSearchActivity extends AppCompatActivity { private MySubscriber mySubscriber; @Override protected void onCreate(Bundle savedInstanceState) { ... mySubscriber = (MySubscriber) getLastCustomNonConfigurationInstance(); if (mySubscriber != null) { mySubscriber.activeActivity = this; } ... @Override public Object onRetainCustomNonConfigurationInstance() { return mySubscriber; } Can implement task sharing, Quite simple & safely
  • 12. 3. Progress dialog private void showProgressDialog() { if (getSupportFragmentManager().findFragmentByTag(TAG_PROGRESS_DIALOG) == null) { new ProgressDialogFragment().show(getSupportFragmentManager(), TAG_PROGRESS_DIALOG); } } public static class ProgressDialogFragment extends AppCompatDialogFragment { @Override protected void onResume() { super.onResume(); // まだ通信の途中だったらdialogを出す if (mySubscriber != null && !mySubscriber.isUnsubscribed()) { showProgressDialog(); } ● Avoid multiple dialog showing ● Subscriber can be a clue if to show progress dialog
  • 13. Summary Introduced my idea to implement simple WebAPI client app We have efficient way in 2016 (databinding, RxJava) Let’s discuss how you design / implement such application now? (Source code intoduced in my presentation) https://github.com/tsuyoyo/roppongi-aar3-address-search-app
  • 14. 2. RxJava Can implement task sharing, Quite simple & safely public class RxZipSearchActivity extends AppCompatActivity { private static class MySubscriber extends Subscriber<List<String>> { private RxZipSearchActivity activeActivity; @Override public void onCompleted() { Log.d("TestTestTest", "onCompleted"); } @Override public void onError(Throwable e) { notifyResult(e.getMessage()); } @Override public void onNext(List<String> result) { activeActivity.notifyResult(result); onCompleted();

Notas del editor

  1. 早速ですが、みなさんはこの画面をどうやって実装しますか? 郵便番号を入力するフォームがあって、Searchボタンがあります 7桁の郵便番号を入力してボタンを押すと、「Please wait...」のダイアログを出して、住所検索のWebAPIを叩きます 検索結果である住所がこのように表示されます。 通信の最中、画面を回転しても裏に行っても通信をキャンセルせず、結果をきちんと表示します。 恐らく皆さん様々な実装方法を思い浮かべたと思います。 ということで、これをツマミにお酒が進みそうな予感がプンプンしてきたのではないでしょうか。 ではこれから、ワタクシなりの2016年の今だからこうやって実装する、という話をしていきたいと思います。
  2. 早速ですが、みなさんはこの画面をどうやって実装しますか? 郵便番号を入力するフォームがあって、Searchボタンがあります 7桁の郵便番号を入力してボタンを押すと、「Please wait...」のダイアログを出して、住所検索のWebAPIを叩きます 検索結果である住所がこのように表示されます。 通信の最中、画面を回転しても裏に行っても通信をキャンセルせず、結果をきちんと表示します。 恐らく皆さん様々な実装方法を思い浮かべたと思います。 ではこれから、ワタクシなりの2016年の今だからこうやって実装する、という話をしていきたいと思います。
  3. さて、実装にあたって色々あると思うんですが、やっぱり誰もが直面する最大の問題、それは、 縦横回転をどうやって実装するか? ということでは無いでしょうか。縦横回転を行うとActivityの再生成が走る、とか分けわかんねーよと文句を言った経験、 Androidエンジニアには必ずと言っていいほど有ると思います。 このプレゼンでは、この点にフォーカスしてお話をしたいと思います。
  4. しかし時は2016年、この問題をシンプルに解決するための3つの兵器を我々は持っています。 Databinding RxJava そしてActivity と Fragmentのlifecycleです
  5. まずDatabindingですが、XMLとDataModelを bind させる機能ですね。 ObservableFieldというものをmodelに含めることで、javaのコードからmodelの値を変更すれば自動的に画面も変わるようになりました。 さらに、双方向bindingというものを使えば、例えばEditTextで入力された値がModelの中のvalueの値に反映されるようになります。
  6. そしてポイントは Modelの中のObservableFieldなのですが、このクラスはserializableを継承しています。 よって、”Viewの状態”を、このようにonSaveInstaceStateを用いることで極めて簡単にActivity間を行き来することができます。
  7. mySubscriberを縦横で引き継ぐメリット 小さい実装のものだけでいい (引き継ぎがムダに複雑にならない) isUnsubscribeで状態が分かる
  8. mySubscriberを縦横で引き継ぐメリット 小さい実装のものだけでいい (引き継ぎがムダに複雑にならない) isUnsubscribeで状態が分かる
  9. mySubscriberを縦横で引き継ぐメリット 小さい実装のものだけでいい (引き継ぎがムダに複雑にならない) isUnsubscribeで状態が分かる
  10. まとめ ・DIの学習法Tipsでした ・Spring bootというフレームワークを使って、小さなWebサーバアプリをつくるのがオススメ ・Daggerに興味があるけど馴染めないなぁと困ってる方にオススメします ・オフィシャルのリンクを。
  11. mySubscriberを縦横で引き継ぐメリット 小さい実装のものだけでいい (引き継ぎがムダに複雑にならない) isUnsubscribeで状態が分かる