How to get hosting Activity from a view?
AndroidAndroid ActivityAndroid EdittextAndroid ViewAndroid Problem Overview
I have an Activity
with 3 EditText
s and a custom view which acts a specialised keyboard to add information into the EditText
s.
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
Activity host = (Activity) view.getContext()
; andview.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; } ... ... }