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
SourceGeneratorのススメ
htkym
0
200
AIエージェントのキホンから学ぶ「エージェンティックコーディング」実践入門
masahiro_nishimi
5
460
Oxlint JS plugins
kazupon
1
960
Patterns of Patterns
denyspoltorak
0
1.4k
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
1k
2026年 エンジニアリング自己学習法
yumechi
0
130
15年続くIoTサービスのSREエンジニアが挑む分散トレーシング導入
melonps
2
200
Vibe Coding - AI 驅動的軟體開發
mickyp100
0
180
フロントエンド開発の勘所 -複数事業を経験して見えた判断軸の違い-
heimusu
7
2.8k
CSC307 Lecture 02
javiergs
PRO
1
780
責任感のあるCloudWatchアラームを設計しよう
akihisaikeda
3
170
Unicodeどうしてる? PHPから見たUnicode対応と他言語での対応についてのお伺い
youkidearitai
PRO
1
2.5k
Featured
See All Featured
Visualization
eitanlees
150
17k
Context Engineering - Making Every Token Count
addyosmani
9
660
Git: the NoSQL Database
bkeepers
PRO
432
66k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.2k
Unsuck your backbone
ammeep
671
58k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
jQuery: Nuts, Bolts and Bling
dougneiner
65
8.4k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
1
1.4k
HDC tutorial
michielstock
1
380
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.4k
Ruling the World: When Life Gets Gamed
codingconduct
0
140
Building an army of robots
kneath
306
46k
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