Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getOwnerActivity returns null in custom dialog

Tags:

android

dialog

I write a custom dialog and try to get some data from its parent activity, but I always get null when I call getOwnerActivity, could anyone tell me why this happen? Why I can show the data in the DemoDialog while failed to show data from TestDialogActivity?

Many thanks in advance.

DialogTestActivity

public class DialogTestActivity extends Activity {
    List<String> data = new ArrayList<String>();
    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                showDialog(0);
            }
        });
    }

    public List<String> getData(){
        data.add("one");
        data.add("two");
        data.add("three");
        return data;
    }

    public Dialog onCreateDialog(int id){
        return new DemoDialog(this);
    }
}

DemoDialog

public class DemoDialog extends Dialog {
    Context context;

    public DemoDialog(Context context) {
        super(context);
        setContentView(R.layout.dialog);
        this.context = context;
        setTitle("Delete City");
        ListView list = (ListView)findViewById(R.id.list);
        ArrayAdapter<String> aa = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_multiple_choice, ((DialogTestActivity)getOwnerActivity()).getData());
//      ArrayAdapter<String> aa = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_multiple_choice, getData());
        list.setAdapter(aa);
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }

    private List<String> getData(){
        List<String> data = new ArrayList<String>();
        data.add("1");
        data.add("2");
        return data;
    }
}
like image 991
eric2323223 Avatar asked Sep 25 '11 01:09

eric2323223


People also ask

What is setCancelable?

setCancelable(false) means that back key doesn't close the dialog. If for example you want the dialog to be closed only if the user click some dialog button then both setCanceledOnTouchOutside() and setCancelable() should be set to false. Follow this answer to receive notifications.

Which method is used to hide the dialog?

hide() will just change the visibility status of the dialog but the object will be still there and can be shown again using show() method. dismiss() hides and also destroys the dialog.

What are the dialog classes in Android?

The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses: AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.


2 Answers

I tried to use getOwnerActivity() method in all possible methods of my custom Dialog. It always returns null (Android 2.3). Then I checked its source code and the activity it returns is set only in setOwnerActivity(Activity activity) which is not called anywhere. So if you want getOwnerActivity() to return value different than null, you have to do this:

public MyCustomDialog(Context context) {
    super(context);
    if (context instanceof Activity) {
        setOwnerActivity((Activity) context);
    }
} 
like image 125
Mario Kutlev Avatar answered Nov 12 '22 16:11

Mario Kutlev


If you think about the situation, you will understand why. When you call new DemoDialog(this), you execute all the code in the constructor. After that, you return it from onCreateDialog and Android does its magic. If you try to get owner from the constructor, Android hasn't hooked it yet, so you have no owner yet.

So you can do either of these:

public class DemoDialog extends Dialog {
    public DemoDialog(Context context) {
        super(context);

        // chances of context not being an activity is very low, but better to check.
        Activity owner = (context instanceof Activity) ? (Activity)context : null;
        if (owner != null) {
            // owner activity is defined here
        }
    }

    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        // getOwnerActivity() should be defined here if called via showDialog(), so do the related init here
        Activity owner = getOwnerActivity();
        if (owner != null) {
            // owner activity defined here
        }
    }
}

Note that the second method is preferred because

like image 36
Sajid Avatar answered Nov 12 '22 15:11

Sajid