I am running foreground service that uses packageName
from MainActivity. Way I implement it is I create static variable instance
in the MainActivity. It gets populated with value inside onCreate, onStart and onResume. Then I access MainActivty.instance.packageName
from foreground service.
Sometime I see exceptions (when app comes back from background or sleep mode) that complains that MainActivty.instance
is null when accessed from my foreground service. In theory it should not happen since Activity should be activated first which in turn will set MainActivty.instance
. How is it possible?
UPDATE: I also use singleton MainActivity from other services and helper/utility classes to do following: to open MaterialDialog, getContentResolver(), getPackageManager(), getSystemService(), startActivityForResult(). So its very convenient to have access to my MainActivity from everywhere.
Would love to hear better suggestions.