Simple Step to Listen Your Activity and Fragment Lifecycle Event in Seconds | Part 2

1 simple setup for globally log you Android Activity and Fragment, Imagine how simple is it! This is second part of previous article that I’m continue work on and make it a small library.

Farhan Yuda Pahlevi
3 min readFeb 8, 2020
Source : wallpaperplay.com

Several weeks a go I made an article about activity lifecycle listener. Then I start some research how to listen fragment lifecycle, but I’m trying to avoid to use LifecycleObserver, that one is part of AndroidJetpack you can read more about it here. My research give me some result and now I can listen to fragment lifecycle too.

Introducing, Jotter

Ya, finally I’m able listening to activity and fragment event at the same time without adding one by one ~you can read here in my previous article~. After that I wrap all of my code into an Android Library so others devs can use it my their way. Why I name it Jotter? Just because it feels like you can write your whole app lifecycle event no matter it is activity or fragment.

How To Use

I’m trying to make it very simple, if you want to use Jotter make sure you already make an Application class and already registered in AndroidManifest. Since I publish it in github, what you need is to put this declare maven jitpack inside your build.gradle in project level :

maven { url "https://jitpack.io" }

Then declare Jotter in your build.gradle application level :

implementation 'com.github.pahlevikun:jotter:1.0.0'

Next step you needed is just initialize Jotter inside onCreate at application class like this:

Jotter
.Builder(this)
.setLogEnable(true)
.setActivityEventFilter(listOf(ActivityEvent.CREATE, ActivityEvent.DESTROY))
.setFragmentEventFilter(listOf(FragmentEvent.VIEW_CREATE, FragmentEvent.PAUSE))
.setJotterListener(JotterListener)
.build()
.startListening()

Jotter use Builder pattern so you can use chaining method like that, don’t forget to put your JotterListener and call startListening method. For more info about this library, please visit my github.

Feature

For now Jotter only have several feature like

  1. Listen to Activity Lifecycle
  2. Listen to Fragment Lifecycle
  3. Logging into your logcat
  4. Filtering your Activity or Fragment event listener

Conclusion

Before doing this, honestly I’m very apathetic with my task and my manager at my office since I only work with some easy stuff and not receiving more trust, including this stuff at my work~logging lifecycle~. I’m trying to do some small research in my previous article here and finally it become open source library. So I hope that it can help many devs outside, and at least I’m happy with that.

That’s all from me, feel free to read and comment. I hope this article help your Android development. If you interest in flutter, you can read my article here in my medium. See you!

--

--

Farhan Yuda Pahlevi

Software Engineer | Senior Mobile Engineer at Gojek — Flutter - Android - iOS - KMM | Google Associate Android Developer 2017–2020 | http://pahlevikun.id