Home » Android » android – getOwnerActivity returns null in custom dialog

android – getOwnerActivity returns null in custom dialog

Posted by: admin May 14, 2020 Leave a comment

Questions:

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;
    }
}
How to&Answers:

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

Answer:

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);
    }
} 

Answer:

You can extends your custom dialog from AppCompatDialog and access to activity by this code:

public static Activity scanForActivity(Context context) {
    if (context == null)
        return null;
    else if (context instanceof Activity)
        return (Activity) context;
    else if (context instanceof ContextWrapper)
        return scanForActivity(((ContextWrapper) context).getBaseContext());
    return null;
}

Answer:

This, below, worked for me.

private Activity activity;

public MyCustomDialog(Activity activity) {
    super(activity);
    this.activity = activity;
} 

Then I use activity instead of getOwnerActivity().