Android DataBinding and Lazy Loading

Aung Ye Htet
1 min readDec 13, 2020

--

Android DataBinding and Lazy Loading

ကျွန်တော့်မှာ မနေတတ်မထိုင်သာဖြစ်တဲ့အကျင့်တစ်ခုရှိပါတယ်..

အဲ့တာကတော့ ကိုယ်တိုင် ခက်ခက်ခဲခဲ ကြိုးစားပြီးမှ သဘောပေါက်နားလည်လိုက်တဲ့ အကြောင်းအရာတစ်ခုကို တခြားသူတစ်ယောက်ကို ပျော်ပျော်ကြီး ရှင်းပြပေးချင်တာပါပဲ..

ဒါပေမဲ့ တခါတလေ ဘယ်မှာပြဿနာတက်သလဲဆို ကိုယ်တိုင် Apply မလုပ်ရသေးတာတွေ Shareပေး လိုက်မိတာမျိုး။ အဲ့ မှာ ကိုယ့်အမြင်တွေပါ ရောထည့်ပေးမိတာတွေ ပြန်ဖတ်ခါမှ သတိထားမိတယ်.. အမှားတွေပေါ့လေ

အခုလဲ DataBinding နဲ့ Fragmentတွဲ သုံးတဲ့အခါမှာ ပြဿနာလေး တစ်ခုရှိတာ ကိုယ်တိုင်ကြုံဖူးပြီး သိရတော့ တဖန် Shareပြန်ရဦးမယ်

Fragmentမှာ Binding သုံးတဲ့အခါမှာ

- Lazy Loading ကို ဖြစ်နိုင်ရင် မသုံးဖို့နဲ့

- onDestroyView မှာ Binding ကို nullပေးဖို့ပါ။

ဘာလို့ Lazy ရှောင်သင့်သလဲ?

အခုနောင်ပိုင်း Android App တွေမှာ Single Activity နဲ့ ထားသုံးကြတော့ Fragment တွေ အသုံးများလာတယ်။ အဲ့ကောင်ရဲ့ Lifecycleက ကိုးရို့ကားရားနိုင်လွန်းတယ်။ ဘယ်လိုလဲဆို Back Key နဲ့ ပြန်ဝင်လာတဲ့အခါ onCreate ကနေ ပြန်စတာကိုပါပဲ။

ဆိုတော့ Fragment Object ကြီးကိုကျတော့ အဟောင်းတိုင်း ပြန်သုံးပေမဲ့ Callback တွေကိုကျ အစကနေပြန်စတော့ ဆိုင်ရာ View တွေက ပြဿနာတက်ပါ လေရော။

သဘောက binding ကို Lazy နဲ့ထားပြီး တစ်ခါပဲဆောက်ထားတယ်။ ဒါမဲ့ Lifecycleက အစ‌ကနေပြန်လာတော့ ရှိပြီးတာ Referenceကို ပြန်သုံးမိတဲ့အခါ ဂွမ်းပါလေရော။

ဥပမာ- RecyclerView မှာ addItemDecoration() ကို နောက်တခါပြန်ခေါ်မိတာမျိုး။

ဖြစ်နိုင်ရင် Fragmentမှာ Bindingနဲ့ Lazyကိုတွဲမသုံးစေချင်ပါဘူး။ bindingသည် Nullableဖြစ်သင့်ပါတယ်။ Fragmentရဲ့ onDestroy Viewမှာ null ပေးသင့်ပါတယ်။ Activityမှာတော့ Lifecycle Callback တွေမှန်တာမို့ null မပေးလဲ ရပါတယ်။

DataBinding ရဲ့ unbindကို မခေါ်မိလို့ ဖြစ်တဲ့ issueတော့ ကျွန်တော်လဲ မကြုံဖူးသေးဘူး ။ But. ဒါပေမဲ့ unbind() က

Binding ထဲက Listener တွေ Unregister လုပ်ဖို့ သူ့ထဲမှာ ခေါ်ထား‌တာတွေရတယ်။ဆို‌‌တော့ onDestroyView မှာ unbind လုပ်တာ မမှားဘူးလို့ ယူဆပါတယ်။

Ko Wine Chit Paing ရဲ့ ViewBinding issue တွေ Fixတဲ့အကြောင်း လာပြောပြပေးလို့သာ ကျွန်တော်လဲ သတိထားမိဖြစ်သွား‌‌လေရဲ့။ Recycler Adapterရဲ့ Memory Leakတော့ နောက်ကြုံမှ ထပ် Share တော့မယ်

.

ဒီတခေါက် အမှားတွေပါလဲ ဆူသွားကြပါဦးး

By (Kotlin’s Lazy Hater)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Aung Ye Htet
Aung Ye Htet

Written by Aung Ye Htet

Android Developer, Kotlin Enthusiast

No responses yet

Write a response