bielas
bielas

Reputation: 712

Calling getOwnerActivity() method always return null

I have created a simple custom dialog class. In further code I want to run new Intent:

 Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                    Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345"));
           startActivity(intent);

But the problem is whenever I call to change into that Intent I always get null in getOwnerActivity() - how to properly call that method?

public class AddToQueueDialog extends Dialog implements View.OnClickListener {

    Activity mActivity;
    private final String android_id = Settings.Secure.getString(getContext().getContentResolver(),
            Settings.Secure.ANDROID_ID);

    public Activity getmActivity() {
        return mActivity;
    }

    public void setmActivity(Activity mActivity) {
        this.mActivity = mActivity;
    }

    public AddToQueueDialog(Context context, WashLocation washLocation) {
        super(context);
        setWashLocation(washLocation);
        setmActivity(getOwnerActivity());
    }

Upvotes: 1

Views: 821

Answers (3)

David Wasser
David Wasser

Reputation: 95618

context is the owning Activity. Your constructor is called with context. This is the owning Activity.

Upvotes: 0

Digvijay Singh
Digvijay Singh

Reputation: 621

If you will check the 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 change your constructor like following

 public AddToQueueDialog(Context context, WashLocation washLocation) {
        super(context);
        if (context instanceof Activity) {
             setOwnerActivity((Activity) context);
        }
        setWashLocation(washLocation);
        setmActivity(getOwnerActivity());
    }

Upvotes: 1

jagapathi
jagapathi

Reputation: 1645

You cant call the getOwnerActivity() in Oncreate

If you try to get owner from the constructor, Android hasn't hooked it yet, so you have no owner yet.

try this instead

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

Upvotes: 0

Related Questions