Android - Dialog on top of foreground activity

      One of the annoying prerequisites of a Dialog is its dependency of a valid context. This can be a pain for an application having multiple activities with a lot of book keeping etc. Wish the framework exposed an API to display the dialog with its available info. Unfortunately, until then this extended version of the Application works good enough. Internally, it keeps track of the resumed activity and exposes a API showDialog().

public class DialogApplication extends Application {

    private WeakReference<Context> mActiveContext;
    private ActivityLifecycleCallbacks mRegisteredListeners;

    public DialogApplication() {
        super.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {
                if (mRegisteredListeners != null) {
                    mRegisteredListeners.onActivityCreated(activity, bundle);
                }
            }

            @Override
            public void onActivityStarted(Activity activity) {
                if (mRegisteredListeners != null) {
                    mRegisteredListeners.onActivityStarted(activity);
                }
            }

            @Override
            public void onActivityResumed(Activity activity) {
                if (mRegisteredListeners != null) {
                    mRegisteredListeners.onActivityResumed(activity);
                }
                mActiveContext = new WeakReference<Context>(activity);
            }

            @Override
            public void onActivityPaused(Activity activity) {
                if (mRegisteredListeners != null) {
                    mRegisteredListeners.onActivityPaused(activity);
                }
            }

            @Override
            public void onActivityStopped(Activity activity) {
                if (mRegisteredListeners != null) {
                    mRegisteredListeners.onActivityStopped(activity);
                }
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
                if (mRegisteredListeners != null) {
                    mRegisteredListeners.onActivitySaveInstanceState(activity, bundle);
                }
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                if (mRegisteredListeners != null) {
                    mRegisteredListeners.onActivityDestroyed(activity);
                }
            }
        });
    }

    @Override
    public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
        mRegisteredListeners = callback;
    }

    @Override
    public void unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
        if (mRegisteredListeners == callback) {
            mRegisteredListeners = null;
        }
    }

    public boolean showDialog(CharSequence message) {

        Context activeContext = mActiveContext != null ? mActiveContext.get() : null;

        if (activeContext == null) {
            return false;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(activeContext);
        builder.setMessage(message);
        builder.create().show();
        return true;
    }
}

No comments: