android


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());
}
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());
}
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
}
}
context is the owning Activity. Your constructor is called with context. This is the owning Activity.

Related Links

ValueEventListener() is not reading data before data is updated
Detecting which languages the app has support for
How to add referal to a volley request
how to set color the selected Item of Recycler View?
Video not in resolution i selected
Cannot launch AVD in Emulator [Android Studio 2.2]
HtmlFromHtml overrides text color selector on View
Re-Transition when I click on button (e.g./i.e. Like Intent from current Activity to self Activity)
Android - Marshmallow - Webview Library [closed]
How to check Sqlite databse with colume in Android [duplicate]
Compile Email application from AOSP
How to hide close button in searchview when it's empty android
How to add expandable ListView's child item without dublicate object data listheader
Fresco randomly set uri from cache if drawee view never call .setImageUri(uri)
Get the context of Activity in a Serializable class
Android Tabbed template updating data after call back

Categories

HOME
shell
azure-stream-analytics
time-complexity
ng-idle
solaris
knitr
permissions
regular-language
fosrestbundle
octave
google-chrome-app
android-securityexception
mautic
datastage
jsonpath
onedrive-api
screen-readers
azure-iot-hub
scsi
firefox-addon
git-extensions
asciidoctor-pdf
jquery-ias
exploit
direct3d11
bulletphysics
ntp
kudu
zend-debugger
paragraph
mockjax
reactjs.net
indy10
engine.io
cd
release
demo
cin
p4merge
plaintext
odoo-website
apriori
claims
snapchat
servermanager
scalding
boo
quicktime
jtree
rxtx
atlas
adafruit
xdoclet
jtwig
mobiledoc-kit
phpgrid
rewrite
coda
skspritenode
jsvc
zend-currency
rrule
cakephp-2.7
administration
device-tree
genymotion-call
imagefilter
lnk
wxformbuilder
gnumeric
mutual-authentication
amslidemenu
http-patch
c++builder-xe5
gpars
preload
redis-py
uimanageddocument
extconf.rb
failing-tests
flex-mobile
landscape-portrait
updatesourcetrigger
fail-fast-fail-early
product-management
pbcopy
leader
great-circle

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App