Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6598187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:18:29+00:00 2026-05-25T18:18:29+00:00

I write a custom dialog and try to get some data from its parent

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:18:30+00:00Added an answer on May 25, 2026 at 6:18 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a custom tab with attributes, but I can not get
How can I write a custom IEnumerator<T> implementation which needs to maintain some state
How to write a confirm dialog in JavaScript with custom choices? Instead of just
I'm trying to write a custom in-window confirm dialog (based on jQuery UI) that
Is it necessary or advantageous to write custom connection pooling code when developing applications
I want to write a custom view engine that returns custom text (like coma
I'm trying to write a custom WPF ValidationRule to enforce that a certain property
I'm trying to write a custom function that will let me retrieve a cell
I might be missing something really obvious. I'm trying to write a custom Panel
I am trying to write operator overload for custom class and don't know how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.