Android version of NSNotificationCenter (event binding)

146 Views Asked by At

Is there a way in android to bind to an activity's lifecycle events? I'm making a library and I'd like to hook into onCreate etc. The iOS version of the lib does it like this:

- (void)observe
{
    [self bindAppEvent:UIApplicationDidBecomeActiveNotification toMethod:@selector(applicationDidBecomeActiveHandler:)];
    [self bindAppEvent:UIApplicationWillResignActiveNotification toMethod:@selector(applicationWillResignActiveHandler:)];
    [self bindAppEvent:UIApplicationWillTerminateNotification toMethod:@selector(applicationWillTerminateHandler:)];
}

#pragma mark Private Methods

- (void)bindAppEvent:(NSString *)appEvent toMethod:(SEL)aSelector
{
    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter]
            addObserver:self
               selector:aSelector
                   name:appEvent
                 object:app];
}

thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

You can use Application.registerActivityLifecycleCallbacks to register your own callbacks to activity life-cycle events.

Requirements:

  • API level 14.
  • Your library need a reference the the Application to register the callbacks.