SlideShare a Scribd company logo
1 of 14
Download to read offline
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();

More Related Content

Similar to 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 to 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
 

Recently uploaded

March Patch Tuesday
March Patch TuesdayMarch Patch Tuesday
March Patch TuesdayIvanti
 
From the origin to the future of Open Source model and business
From the origin to the future of  Open Source model and businessFrom the origin to the future of  Open Source model and business
From the origin to the future of Open Source model and businessFrancesco Corti
 
UiPath Studio Web workshop series - Day 2
UiPath Studio Web workshop series - Day 2UiPath Studio Web workshop series - Day 2
UiPath Studio Web workshop series - Day 2DianaGray10
 
2024.03.12 Cost drivers of cultivated meat production.pdf
2024.03.12 Cost drivers of cultivated meat production.pdf2024.03.12 Cost drivers of cultivated meat production.pdf
2024.03.12 Cost drivers of cultivated meat production.pdfThe Good Food Institute
 
Introduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its applicationIntroduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its applicationKnoldus Inc.
 
Novo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4jNovo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4jNeo4j
 
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTSIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTxtailishbaloch
 
LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0DanBrown980551
 
The New Cloud World Order Is FinOps (Slideshow)
The New Cloud World Order Is FinOps (Slideshow)The New Cloud World Order Is FinOps (Slideshow)
The New Cloud World Order Is FinOps (Slideshow)codyslingerland1
 
Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...DianaGray10
 
20140402 - Smart house demo kit
20140402 - Smart house demo kit20140402 - Smart house demo kit
20140402 - Smart house demo kitJamie (Taka) Wang
 
Stobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through TokenizationStobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through TokenizationStobox
 
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - TechWebinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - TechProduct School
 
Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)Muhammad Tiham Siddiqui
 
TrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie WorldTrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie WorldTrustArc
 
Top 10 Squarespace Development Companies
Top 10 Squarespace Development CompaniesTop 10 Squarespace Development Companies
Top 10 Squarespace Development CompaniesTopCSSGallery
 
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInOutage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInThousandEyes
 
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024Alkin Tezuysal
 
My key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAIMy key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAIVijayananda Mohire
 

Recently uploaded (20)

March Patch Tuesday
March Patch TuesdayMarch Patch Tuesday
March Patch Tuesday
 
SheDev 2024
SheDev 2024SheDev 2024
SheDev 2024
 
From the origin to the future of Open Source model and business
From the origin to the future of  Open Source model and businessFrom the origin to the future of  Open Source model and business
From the origin to the future of Open Source model and business
 
UiPath Studio Web workshop series - Day 2
UiPath Studio Web workshop series - Day 2UiPath Studio Web workshop series - Day 2
UiPath Studio Web workshop series - Day 2
 
2024.03.12 Cost drivers of cultivated meat production.pdf
2024.03.12 Cost drivers of cultivated meat production.pdf2024.03.12 Cost drivers of cultivated meat production.pdf
2024.03.12 Cost drivers of cultivated meat production.pdf
 
Introduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its applicationIntroduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its application
 
Novo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4jNovo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4j
 
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTSIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
 
LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0
 
The New Cloud World Order Is FinOps (Slideshow)
The New Cloud World Order Is FinOps (Slideshow)The New Cloud World Order Is FinOps (Slideshow)
The New Cloud World Order Is FinOps (Slideshow)
 
Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...
 
20140402 - Smart house demo kit
20140402 - Smart house demo kit20140402 - Smart house demo kit
20140402 - Smart house demo kit
 
Stobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through TokenizationStobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
 
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - TechWebinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
 
Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)
 
TrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie WorldTrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie World
 
Top 10 Squarespace Development Companies
Top 10 Squarespace Development CompaniesTop 10 Squarespace Development Companies
Top 10 Squarespace Development Companies
 
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInOutage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
 
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
 
My key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAIMy key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAI
 

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();

Editor's Notes

  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で状態が分かる