Similar to this post. Have a custom view (extends EditText
) which must have the ability to call the finish()
method of the parent activity if the user presses the END key.
How can I access the activity
object of the host activity in order to call its finish()
method from within the custom view class?
public class SuppressInputEditText extends androidx.appcompat.widget.AppCompatEditText {
public SuppressInputEditText(Context context) {
super(context);
}
public SuppressInputEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SuppressInputEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return true;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode){
case 6: //end key
//todo: call finish() method of parent activity.
break;
}
return true;
}
}
I can use the getContext()
method of my class, available because it inherits from view
, to get a context, but I don't know how to use that to access the finish()
method. Any help or pointers would be greatly appreciated.
UPDATE: Looking for a solution that can keep the class independent. Thanks!