GetOwnerActivity returns null in a custom dialog

I am writing a user dialog and trying to get some data from its parent activity, but I always get null when I call getOwnerActivity , can someone tell me why this is happening? Why can I show data in DemoDialog until I can show data from TestDialogActivity?

Thank you very much 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; } } 
+8
android dialog
source share
4 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 an owner from the constructor, Android hasn't connected it yet, so you don't have an owner yet.

So you can do one of the following:

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

Please note that the second method is preferred since

+5
source share

I tried to use the 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 action that it returns is set only to setOwnerActivity(Activity activity) , which is not called anywhere. Therefore, if you want getOwnerActivity() return a value other than zero, you must do the following:

 public MyCustomDialog(Context context) { super(context); if (context instanceof Activity) { setOwnerActivity((Activity) context); } } 
+17
source share

You can expand your own dialog from AppCompatDialog and access 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; } 
+1
source share

This, below, worked for me.

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

Then I use activity instead of getOwnerActivity ().

0
source share

All Articles