ActivityLifecycleHelper implementation Description

The ActivityLifecycleHelper uses wrap(Context)to create the class and save a global Application field. It then uses with(ActivityCallbacks) to register android.app.Application.ActivityLifecycleCallbacks on the Application field. Whenever onActivityResumed(Activity) is triggered the class calls a null  OnSaved(Bundle). Whenever onActivitySaveInstanceState(Activity, Bundle) is triggered the class calls a saved/null OnSaved(Bundle). It then unregisters the android.app.Application.ActivityLifecycleCallbacks when onActivityDestroyed(Activity) is triggered. Finally, it returns the base context of the Application field.

ActivityLifecycleHelper.class

public class ActivityLifecycleHelper {

    public interface ActivityCallbacks {
        void OnSaved(Bundle savedInstance);
    }

    private final Application application;

    private ActivityLifecycleHelper(Application application) {
        this.application = application;
    }

    public static ActivityLifecycleHelper wrap(Context context) {
        return new ActivityLifecycleHelper(assertApplication(context));
    }

    public Context with(final ActivityCallbacks callbacks) {
        application.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity a, Bundle b) {}

            @Override
            public void onActivityStarted(Activity a) {}

            @Override
            public void onActivityResumed(Activity a) {
                callbacks.OnSaved(null);
            }

            @Override
            public void onActivityPaused(Activity a) {}

            @Override
            public void onActivityStopped(Activity a) {}

            @Override
            public void onActivitySaveInstanceState(Activity a, Bundle b) {
                callbacks.OnSaved(b);
            }

            @Override
            public void onActivityDestroyed(Activity a) {
                application.unregisterActivityLifecycleCallbacks(this);
            }
        });
        return application.getBaseContext();
    }

    private static Application assertApplication(Context context) {
        final Context application = context.getApplicationContext();
        if (application instanceof Application) return (Application) application;
        throw new NullPointerException("Context must be instance of Application");
    }
}


Usage

public class Usage implements ActivityLifecycleHelper.ActivityCallbacks {

    private final Context context;

    Usage(Context context) {
        this.context = ActivityLifecycleHelper.wrap(context).with(this);
    }

    @Override
    public void OnSaved(Bundle savedInstance) {
        // doingit
    }
}


Reasoning

This is used within a library that when initialized receives a context that will extend from an Application and hooks into any Activity LifeCycle events. 

OnResume() will allow the library to reset some things and onSaveInstanceState() is so the library can rebuild.

Needed

Will this approach register/unregister appropriately? Is anything bad design?
 Will this cause any context leaks?
Yes. It seems like a bad design or I would say not thought through given that you're building a library project which will be used by other apps.

First of all,
in your library initialization, you should ask for Application reference rather than Context reference.

For example,

if your library has an init method like this,

MyLibrary.init(Context applicationContext);

then it should be converted to accept Application object like this,

MyLibrary.init(Application application);

Then,

inside that init method, you should just register your class which is implementing Application.ActivityLifecycleCallbacks. 

application.registerActivityLifecycleCallbacks(ActivityLifecycleHelper.getInstance());

Make sure your class is a singleton. In your example, your ActivityLifecycleHelper class is not a singleton which is definitely a bad design pattern given that you're dealing with global level callbacks and want to initialize only once.

It might look like this,

public class ActivityLifecycleHelper implements Application.ActivityLifecycleCallbacks {
private static ActivityLifecycleHelper ourInstance = new ActivityLifecycleHelper();

private ActivityLifecycleHelper() {
}

public static ActivityLifecycleHelper getInstance() {
    return ourInstance;
}

@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
}

@Override
public void onActivityStarted(Activity activity) {
}

@Override
public void onActivityResumed(Activity activity) {
}

@Override
public void onActivityPaused(Activity activity) {
}

@Override
public void onActivityStopped(Activity activity) {
}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

}

@Override
public void onActivityDestroyed(Activity activity) {

}
}


Also, I don't understand why you have two classes here. What's the use case of Usage class ? Something seems wrong in this two class design.

I hope this helps. Let me know if you want me to elaborate anything.