Monitoring android lifecycle events from a library -


i'm still new programming world in general, hope isn't obvious/abstract question.

i'm developing android library needs monitor lifecycle events of activity that's using it. how accomplish while creating least amount of work developer using library? preferably, use built android.

i've seen similar questions, such as: automatically log android lifecycle events using activitylifecyclecallbacks? doesn't apply library project.

am missing something? insight appreciated.

i think best (long term) option have setup along integration of library (i.e. pass application in entry point of library).

that said, there undocumented way current application. described here: https://stackoverflow.com/a/12495865/458365

try {     final class<?> activitythreadclass =         class.forname("android.app.activitythread");     final method method =      activitythreadclass.getmethod("currentapplication");     return (application) method.invoke(null, (object[]) null); } catch (final exception e) {     // handle exception } 

once have can call application.registeractivitylifecyclecallbacks() register own activitylifecyclecallbacks

update: alternative application (context) i've come across use content provider. think libraries firebase use method because has 0 set up. however, requires consumer of library have application object (which same manual method) lot cleaner:

inside oncreate of cp can cast getcontext application , go there callbacks process.

source: https://medium.com/@andretietz/auto-initialize-your-android-library-2349daf06920


Comments