4

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);
            ...
        }
    }
  1. If MyButton is in e.g. MyActivity I can simply cast context to activity.
  2. Now if MyButton is part of MyDialog (extends Dialog), context.getClass() will point to ContextThemeWrapper and I can not get activity.

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();
}
krisk
  • 237
  • 2
  • 4
  • 10
  • 1
    Please give some context for your question (no pun intended). What do you want to do with the dialog or activity instance? – Code-Apprentice Oct 29 '12 at 20:56
  • For example MyButton can also implement Command interface with execute() method and in some external class I can have onClick() method with Command command = (Command) v; If I click the button, execute() method runs. I need to get to dialog instance from inside MyButton for example to close dialog. I'm just curious/playing after reading this: http://alvinalexander.com/java/java-command-design-pattern-in-java-examples – krisk Oct 29 '12 at 21:17

2 Answers2

20

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.

kikea
  • 1,569
  • 1
  • 13
  • 14
1

I'm don't fully understand what you are doing, but you should be able to get a reference to the Activity from your Dialog with getOwnerActivity().

Perhaps:

public MyButton(Context context, AttributeSet attrs) {
    super(context, attrs);

    Activity activity = getOwnerActivity();
    ...
}
Sam
  • 86,580
  • 20
  • 181
  • 179
  • Unfortunately it does not work. As for (Dialog) context cast, even Eclipse complains. For me it is strange that (Activity) context produces java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity. Anyway thanks for reply. – krisk Oct 30 '12 at 17:39
  • You're right Dialog cannot be a cast to a Context because Dialog doesn't extend Context... But the `getOwnerActivity()` method should work (as long as you open the Dialog as the documentation suggests.) – Sam Oct 30 '12 at 17:50