How to get hosting Activity from a view?

AndroidAndroid ActivityAndroid EdittextAndroid View

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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;
}

Solution 2 - Android

following methods may help you

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

Solution 3 - Android

UPDATED

tailrec fun Context.getActivity(): Activity? = this as? Activity
    ?: (this as? ContextWrapper)?.baseContext?.getActivity()

thanks to @Westy92

Usage:

context.getActivity()

Solution 4 - Android

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;
    }
}

Solution 5 - Android

Just want to note that, if you know that your view is inside a Fragment, you can just do:

FragmentManager.findFragment(this).getActivity();

Call this from your onAttachedToWindow or onDraw, you can't call it earlier (e.g. onFinishInflate) or you will get an exception.

If you are not sure your view is inside a Fragment then refer to other answers e.g. Gomino, just wanted to throw it out there.

Solution 6 - Android

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
    }
}

Solution 7 - Android

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();
}

Solution 8 - Android

@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; } ... ... }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmAndroidView Question on Stackoverflow
Solution 1 - AndroidGominoView Answer on Stackoverflow
Solution 2 - AndroiddiraView Answer on Stackoverflow
Solution 3 - AndroidVladView Answer on Stackoverflow
Solution 4 - AndroidbrownieGirlView Answer on Stackoverflow
Solution 5 - AndroidAdam BurleyView Answer on Stackoverflow
Solution 6 - AndroidFedir TsapanaView Answer on Stackoverflow
Solution 7 - AndroidSebas LGView Answer on Stackoverflow
Solution 8 - AndroidTomcolins Cox Violet CHilton cView Answer on Stackoverflow