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  w ww .j a va 2  s.co m*/
            if (ctx instanceof ContextWrapper) {
                ContextWrapper cw = (ContextWrapper) ctx;
                return findActivityFromContext(cw.getBaseContext());
            }
        }
        return null;
    }


}

Related Tutorials