find Activity From Context - Android Activity

Android examples for Activity:Activity Feature

Description

find Activity From Context

Demo Code


//package com.java2s;
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;
            }// ww  w.j  a v a2  s.c  om
            if (ctx instanceof ContextWrapper) {
                ContextWrapper cw = (ContextWrapper) ctx;
                return findActivityFromContext(cw.getBaseContext());
            }
        }
        return null;
    }
}

Related Tutorials