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
Inside LifecycleObserver
Search
kobito-kaba
July 18, 2018
0
80
Inside LifecycleObserver
How to handle components' lifecycle in Android app developing.
kobito-kaba
July 18, 2018
Tweet
Share
More Decks by kobito-kaba
See All by kobito-kaba
新規プロジェクトでやってよかったことまとめ
kobitokaba
1
720
Youtube like BottomNavigation
kobitokaba
0
180
Modularizing and Refactoring Android App with Clean Architecture
kobitokaba
0
210
Slice Your App
kobitokaba
2
1.1k
Inside Jetpack
kobitokaba
2
91
Generating and Transforming Kotlin code
kobitokaba
0
64
Conference Tourism
kobitokaba
0
230
Inside Jetpack Architecture Components
kobitokaba
0
190
Kotlinもう一歩
kobitokaba
8
15k
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
1
230
We Have a Design System, Now What?
morganepeng
48
7.1k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
Creatively Recalculating Your Daily Design Routine
revolveconf
216
12k
The Brand Is Dead. Long Live the Brand.
mthomps
53
38k
Building Flexible Design Systems
yeseniaperezcruz
326
38k
It's Worth the Effort
3n
182
27k
A designer walks into a library…
pauljervisheath
201
24k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
90
16k
Designing for Performance
lara
604
68k
Designing on Purpose - Digital PM Summit 2013
jponch
114
6.9k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
363
22k
Transcript
Hiroyuki Mori @moridroid Inside LifecycleObserver
Lifecycle Problems in Android
in early days https://developer.android.com/guide/components/activities/activity-lifecycle.html
today Steve Pomeroy https://github.com/xxv/android-lifecycle CC-BY-SA 4.0
case 1: fetching data every onCreate
case 1: fetching data every onCreate
case 1: fetching data every onCreate
case 1: fetching data every onCreate
case 1: fetching data every onCreate
case 1: fetching data every onCreate Wasteful
case 2: showing after activity’s death
case 2: showing after activity’s death
case 2: showing after activity’s death
case 2: showing after activity’s death Crash !!
case 3: forgetting to stop observing
case 3: forgetting to stop observing
case 3: forgetting to stop observing
case 3: forgetting to stop observing memory leak
Handling lifecycles
Lifecycle library
Observer pattern
None
None
None
None
None
None
×
None
None
None
loose coupled
None
None
Event Channel
None
notify events
observe
None
Inside Lifecycle library
subscribing lifecycle events
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {
... lifecycle.addObserver(yourObservable) } }
public class LifecycleRegistry extends Lifecycle { @Override public void addObserver(@NonNull
LifecycleObserver observer) { State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED; ObserverWithState statefulObserver = new ObserverWithState(observer, initialState); ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver);
public class LifecycleRegistry extends Lifecycle { @Override public void addObserver(@NonNull
LifecycleObserver observer) { State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED; ObserverWithState statefulObserver = new ObserverWithState(observer, initialState); ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver);
static class ObserverWithState { ... ObserverWithState(LifecycleObserver observer, State initialState) {
mLifecycleObserver = Lifecycling.getCallback(observer); ... } void dispatchEvent(LifecycleOwner owner, Event event) { ... mLifecycleObserver.onStateChanged(owner, event); ... } }
static class ObserverWithState { ... ObserverWithState(LifecycleObserver observer, State initialState) {
mLifecycleObserver = Lifecycling.getCallback(observer); ... } void dispatchEvent(LifecycleOwner owner, Event event) { ... mLifecycleObserver.onStateChanged(owner, event); ... } }
publishing lifecycle events
public class SupportActivity extends Activity implements LifecycleOwner { ... protected
void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ReportFragment.injectIfNeededIn(this); } ... }
public class ReportFragment extends Fragment { ... public void onActivityCreated(Bundle
savedInstanceState) { dispatch(Lifecycle.Event.ON_CREATE); } public void onStart() { dispatch(Lifecycle.Event.ON_START); } public void onResume() { dispatch(Lifecycle.Event.ON_RESUME); } public void onPause() { dispatch(Lifecycle.Event.ON_PAUSE); } public void onStop() { dispatch(Lifecycle.Event.ON_STOP); } ... }
public class ReportFragment extends Fragment { ... private void dispatch(Lifecycle.Event
event) { ... if (activity instanceof LifecycleOwner) { Lifecycle lifecycle = ((LifecycleOwner) activity).getLifecycle(); if (lifecycle instanceof LifecycleRegistry) { ((LifecycleRegistry) lifecycle).handleLifecycleEvent(event); } } } }
ObserverWithState AdapterObserver Adapter FooObserver ReportFragment SupportActivity LifecycleRegistry addObserver attach dispatch
event sync
ReportFragment SupportActivity LifecycleRegistry attach dispatch event
FooObserver ReportFragment SupportActivity LifecycleRegistry addObserver attach dispatch event
ObserverWithState AdapterObserver Adapter FooObserver ReportFragment SupportActivity LifecycleRegistry addObserver attach dispatch
event sync
Subscriber Publisher EventChannel subscribe publish event fire event
How to use
example: Observing network state
None
None
without LifecycleObserver
class NetworkStateReceiver(private val callback : (NetworkInfo?) -> Unit) : BroadcastReceiver()
{ override fun onReceive(context: Context?, intent: Intent?) { val conn = context?.getSystemService(ConnectivityManager::class.java) ?: return callback(conn.activeNetworkInfo) } } Step1 : Create a BroadcastReceiver
receiver = NetworkStateReceiver { if (it != null && it.isConnected)
{ textView.text = "online" } else { textView.text = "offline" } } Step2 : create an instance create an instance
Step3 : register it at onResume override fun onResume() {
super.onResume() val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION) registerReceiver(receiver, filter) } register at onResume
override fun onPause() { super.onPause() unregisterReceiver(receiver) } Step4 : unregister
it at onPause unregister at onPause
override fun onPause() { super.onPause() unregisterReceiver(receiver) } Step5 : Never
forget to unregister unregister at onPause Don’t miss it
with LifecycleObserver
enum class Connectivity { OFFLINE, ONLINE } class NetworkObservable( private
var activity: Activity?, private val callback : (Connectivity) -> Unit) : LifecycleObserver { private val receiver = NetworkStateReceiver { if (it != null && it.isConnected) { callback(Connectivity.ONLINE) } else { callback(Connectivity.OFFLINE) } } } Step1 : Wrap the receiver
enum class Connectivity { OFFLINE, ONLINE } class NetworkObservable( private
var activity: Activity?, private val callback : (Connectivity) -> Unit) : LifecycleObserver { private val receiver = NetworkStateReceiver { if (it != null && it.isConnected) { callback(Connectivity.ONLINE) } else { callback(Connectivity.OFFLINE) } } } Step1 : Wrap the receiver implements LifecycleObserver
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun registerReceiver() { activity?.registerReceiver(receiver, IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)) } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun unregisterReceiver()
{ activity?.unregisterReceiver(receiver) } @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) fun release() { activity = null } Step2 : register / unregister at lifecycle events
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun registerReceiver() { activity?.registerReceiver(receiver, IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)) } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun unregisterReceiver()
{ activity?.unregisterReceiver(receiver) } @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) fun release() { activity = null } Step2 : register / unregister at lifecycle events @OnLifecycleEvent
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {
... lifecycle.addObserver( NetworkObservable(this) { if (it == Connectivity.ONLINE) { textView.text = "online" } else { textView.text = "offline" } }) } } Step3 : Use it !
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {
... lifecycle.addObserver( NetworkObservable(this) { if (it == Connectivity.ONLINE) { textView.text = "online" } else { textView.text = "offline" } }) } } Step3 : Use it ! easy to use
Conclusion
Lifecycle Management
Lifecycle Management - is a hard and hassle task
Lifecycle Management - is a hard and hassle task -
can cause problems
Lifecycle Management - is a hard and hassle task -
can cause problems The Lifecycle library
Lifecycle Management - is a hard and hassle task -
can cause problems The Lifecycle library - provides the feature to observe lifecycles
Lifecycle Management - is a hard and hassle task -
can cause problems The Lifecycle library - provides the feature to observe lifecycles - can create lifecycle-aware components easily
Hiroyuki Mori @moridroid Thank you