Skip to content

Instantly share code, notes, and snippets.

@keroxp
Last active June 27, 2016 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keroxp/4d4bdd0731b403b20cd0646d116443e7 to your computer and use it in GitHub Desktop.
Save keroxp/4d4bdd0731b403b20cd0646d116443e7 to your computer and use it in GitHub Desktop.
Fragment that logs lifecycle event.
import android.app.Activity
import android.app.Fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
abstract class BaseFragment : Fragment() {
fun <O> Any.wrap(tag: String, body: () -> O): O {
val klass = this.javaClass.simpleName
Log.d(klass, "---$tag-->")
val ret = body()
Log.d(klass, "<==$tag===")
return ret
}
abstract val self: Fragment
fun onResume(body: () -> Unit) {
self.wrap("onResume") {
super.onResume()
body()
}
}
fun onSaveInstanceState(outState: Bundle?, body: () -> Unit) {
self.wrap("onSaveInstanceState") {
super.onSaveInstanceState(outState)
body()
}
}
fun onDetach(body: () -> Unit) {
self.wrap("onDetach") {
super.onDetach()
body()
}
}
fun onAttach(activity: Activity?, body: () -> Unit) {
self.wrap("onAttach") {
super.onAttach(activity)
body()
}
}
fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?, body: () -> View?): View? {
return self.wrap("onCreateView") {
super.onCreateView(inflater, container, savedInstanceState)
body()
}
}
fun onPause(body: () -> Unit) {
self.wrap("onPause") {
super.onPause()
body()
}
}
fun onDestroyView(body: () -> Unit) {
self.wrap("onDestroyView") {
super.onDestroyView()
body()
}
}
fun onViewCreated(view: View?, savedInstanceState: Bundle?, body: () -> Unit) {
self.wrap("onViewCreated") {
super.onViewCreated(view, savedInstanceState)
body()
}
}
fun onActivityCreated(savedInstanceState: Bundle?, body: () -> Unit) {
self.wrap("onActivityCreated") {
super.onActivityCreated(savedInstanceState)
body()
}
}
fun onStop(body: () -> Unit) {
self.wrap("onStop") {
super.onStop()
body()
}
}
fun onCreate(savedInstanceState: Bundle?, body: () -> Unit) {
self.wrap("onCreate") {
super.onCreate(savedInstanceState)
body()
}
}
fun onStart(body: () -> Unit) {
self.wrap("onStart") {
super.onStart()
body()
}
}
fun onViewStateRestored(savedInstanceState: Bundle?, body: () -> Unit) {
self.wrap("onViewStateRestored") {
super.onViewStateRestored(savedInstanceState)
body()
}
}
fun onDestroy(body: () -> Unit) {
self.wrap("onDestroy") {
super.onDestroy()
body()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment