Last active
June 27, 2016 08:12
-
-
Save keroxp/4d4bdd0731b403b20cd0646d116443e7 to your computer and use it in GitHub Desktop.
Fragment that logs lifecycle event.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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