find Activity From Context - Android android.app

Android examples for android.app:Activity

Description

find Activity From Context

Demo Code

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;

public class Main{

    public static Activity findActivityFromContext(Context ctx) {
        if (ctx != null) {
            if (ctx instanceof Activity) {
                return (Activity) ctx;
            }/*from   ww  w.j  a  v a 2 s. c  om*/
            if (ctx instanceof ContextWrapper) {
                ContextWrapper cw = (ContextWrapper) ctx;
                return findActivityFromContext(cw.getBaseContext());
            }
        }
        return null;
    }


}

Related Tutorials