Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Parcerler
Search
York Wu
January 18, 2017
Programming
0
75
Parcerler
GDG Kaohsiung 2017/01/18
York Wu
January 18, 2017
Tweet
Share
More Decks by York Wu
See All by York Wu
Clean Android Code for Unit Testing
yorkwu0318
0
65
Other Decks in Programming
See All in Programming
CSC307 Lecture 02
javiergs
PRO
1
780
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
230
Fluid Templating in TYPO3 14
s2b
0
130
Oxlint JS plugins
kazupon
1
940
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
1k
プロダクトオーナーから見たSOC2 _SOC2ゆるミートアップ#2
kekekenta
0
210
AI巻き込み型コードレビューのススメ
nealle
1
230
AIによる高速開発をどう制御するか? ガードレール設置で開発速度と品質を両立させたチームの事例
tonkotsuboy_com
7
2.3k
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
230
Best-Practices-for-Cortex-Analyst-and-AI-Agent
ryotaroikeda
1
110
CSC307 Lecture 01
javiergs
PRO
0
690
責任感のあるCloudWatchアラームを設計しよう
akihisaikeda
3
170
Featured
See All Featured
The World Runs on Bad Software
bkeepers
PRO
72
12k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
140
Joys of Absence: A Defence of Solitary Play
codingconduct
1
290
Leo the Paperboy
mayatellez
4
1.4k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
8.6k
A better future with KSS
kneath
240
18k
KATA
mclloyd
PRO
34
15k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
290
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.9k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.6k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.7k
Transcript
Parceler 2017/01/18 GDGK
Guideline Parcelable • Why use parcelable • How use parcelable
Parceler 簡介
Send string in an intent / bundle Intent intent =
new Intent(this, TargetActivity.class); intent.putExtra("intent_name", "york"); startActivity(intent);
send object in an intent / bundle Intent intent =
new Intent(this, TargetActivity.class); intent.putExtra("intent_user_data", userData); startActivity(intent);
Intent intent = new Intent(this, TargetActivity.class); intent.putExtra("intent_user_data", userData); startActivity(intent); send
object in an intent / bundle
send object in an intent / bundle Intent intent =
new Intent(this, TargetActivity.class); intent.putExtra("intent_user_data", userData); startActivity(intent);
Serializable http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
Serializable 實作
Serializable 實作 Simplicity
Parcelable 實作
Parcelable 實作 boilerplate code
Serializable vs Parcelable http://www.developerphil.com/parcelable-vs-serializable/
Serializable vs Parcelable Serializable Parcelable 效能 低 高 維護性 高
低 用途 stream, file... intent / bundle
Other way? @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
UserData userData = MySingleton.getUserData();
Other way? @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
UserData userData = MySingleton.getUserData();
use global data (static) UserData userData = MySingleton.getUserData(); Benefit: •
easy to use • high performance
use global data (static) Disadvantage: • high coupling • can’t
handle activity life cycle (system kill)
Activity Life Cycle
Activity Life Cycle system kill
Activity Life Cycle Activity A MySingleton.setUserData(userData);
Activity Life Cycle Activity A Activity B UserData userData =
MySingleton.getUserData();
Activity Life Cycle Activity A Activity B UserData userData =
MySingleton.getUserData(); // userData is null After system kill
使用時機 • Intent / Bundle • Activity Life Cycle -
◦ system kill ◦ recreate
Activity Life Cycle recreate
Activity Life Cycle recreate onConfigurationChange • screen orientation • multiple-windows
• language change • ...
handle recreate @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
if (savedInstanceState != null) { userData = savedInstanceState.getParcelable(STATE_USER_DATA); } else { presenter.loadUserData(); } }
handle recreate @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelable(STATE_USER_DATA,
userData)); }
Parcelable 實作 - 純手動
Parcelable 實作 - 半手動 https://github.com/mcharmas/and roid-parcelable-intellij-plugin
Parcelable 實作 - 全自動 https://github.com/johncarl81/ parceler
Guideline Parcelable Parceler 簡介 • 基本用法 • 進階用法
Parceler 基本 用法 @Parcel
Parceler 基本用法 Parcels.unwrap() Parcels.wrap()
Parceler 基本用法 @Transient
Parceler 進階用法 @ParcelConverter
Parceler 進階用法 @ParcelConverter
Parceler 進階用法 @Parcel(analyze = DetailUserData.class)
更多用法 https://github.com/johncarl81/parceler
Q & A