Home » Android » android – How to get hosting Activity from a view?

android – How to get hosting Activity from a view?

Posted by: admin March 10, 2020 Leave a comment

Questions:

I have an Activity with 3 EditTexts and a custom view which acts a specialised keyboard to add information into the EditTexts.

Currently I’m passing the Activity into the view so that I can get the currently focused edit text and update the contents from the custom keyboard.

Is there a way of referencing the parent activity and getting the currently focused EditText without passing the activity into the view?

How to&Answers:

I just pulled that source code from the MediaRouter in the official support library and so far it works fine:

private Activity getActivity() {
    Context context = getContext();
    while (context instanceof ContextWrapper) {
        if (context instanceof Activity) {
            return (Activity)context;
        }
        context = ((ContextWrapper)context).getBaseContext();
    }
    return null;
}

Answer:

following methods may help you

  1. Activity host = (Activity) view.getContext(); and
  2. view.isFocused()

Answer:

I took Gomino‘s answer and modified it to fit perfectly in myUtils.java so I can use it wherever and whenever I need. Hope someone finds it helpful 🙂

abstract class myUtils {
    public static Activity getActivity(View view) {
        Context context = view.getContext();
        while (context instanceof ContextWrapper) {
            if (context instanceof Activity) {
                return (Activity)context;
            }
            context = ((ContextWrapper)context).getBaseContext();
        }
        return null;
    }
}

Answer:

I like this solution written in Kotlin

tailrec fun Context?.activity(): Activity? = when (this) {
    is Activity -> this
    else -> (this as? ContextWrapper)?.baseContext?.activity()
}

Usage in View class

context.activity()

Decompiled code:

public static final Activity activity(Context context) {
    while (!(context instanceof Activity)) {
        if (!(context instanceof ContextWrapper)) {
            context = null;
        }
        ContextWrapper contextWrapper = (ContextWrapper) context;
        if (contextWrapper == null) {
            return null;
        }
        context = contextWrapper.getBaseContext();
        if (context == null) {
            return null;
        }
    }
    return (Activity) context;
}

Answer:

In Android 7+ the view does not have access to the enclosing activity anymore, so view.getContext() can’t be cast to an Activity anymore.

Instead, the code below works in Android 7+ and 6:

private static Activity getActivity(final View view) {
    return (Activity) view.findViewById(android.R.id.content).getContext();
}

Answer:

Kotlin extension property for View to retrieve parent activity:

val View.activity: Activity?
get() {
    var ctx = context
    while (true) {
        if (!ContextWrapper::class.java.isInstance(ctx)) {
            return null
        }
        if (Activity::class.java.isInstance(ctx)) {
            return ctx as Activity
        }
        ctx = (ctx as ContextWrapper).baseContext
    }
}

Answer:

@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if(request.getUrl().getHost().startsWith(“pay.google.com”)) {
Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
view.getContext().startActivity(intent);
return true;
}


}

Answer:

For me this worked:

Context context = SdkUtils.getRootViewContext(getRootView());

and then casting like this (Activity) context