Maybe I'm missing sth here but here it is. Let say I extended Button
public class MyButton extends Button {
...
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
...
}
}
So how can I get instance of dialog or activity in the second case?
EDIT Ok more code to better illustrate what I wanted to do:
public class MyDialog extends Dialog {
private MyButton myButton;
public MyDialog(Context context) {
super(context)
this.setContentView(R.layout.my_dialog);
this.setTitle("My Dialog");
myButton = (MyButton) findViewById(R.id.my_button);
}
}
public class MyButton extends Button implements Command {
private MyActivity myActivity;
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
System.out.println(context instanceof ContextThemeWrapper); // TRUE
System.out.println(context instanceof Activity); // FALSE
myActivity = ??? // or myDialog = ???
}
@Override
public void execute() {
MyDialog myDialog = myActivity.getMyDialog();
myDialog.cancel();
}
}
and somewhere in other class after connecting listener:
@Override
public void onClick(View v) {
Command command = (Command) v;
command.execute();
}
I had similar situation and I solve my case wit this snippet:
private static Activity scanForActivity(Context cont) {
if (cont == null)
return null;
else if (cont instanceof Activity)
return (Activity)cont;
else if (cont instanceof ContextWrapper)
return scanForActivity(((ContextWrapper)cont).getBaseContext());
return null;
}
Hope that this can help somebody.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With