Note that there are some explanatory texts on larger screens.

plurals
  1. POConnect to service from abstract superclass in Android
    text
    copied!<p>I am making an application with various activities that all require access to the same database.</p> <p>I've set up an abstract class that has some common elements of all activities (notably the app-specific title bar and some of its behavior, as well as some repeated code and variables).</p> <p>I've set up access to the database through a service. I feel that this service should only ever have one instance.</p> <p>The recommended way to interact and communicate with a server seems to be with binding.</p> <p>That seems a bit cumbersome though (as you need to specify two additional classes - Binder and ServiceConnection), though admmittedly, it all works very well.</p> <p>This is where I run into my issue though: the binding is activity specific. I can query the db from the shared abstract class code, but I can't seem to do the same from subclass code.</p> <p>Code as it appears in my abstract class:</p> <pre><code>protected static DatabaseService autoservice; protected static boolean serviceBound = false; protected static ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder service) { AutoBinder binder = (AutoBinder) service; autoservice = binder.getService(); serviceBound = true; } @Override public void onServiceDisconnected(ComponentName arg0) { serviceBound = false; } }; </code></pre> <p>If I check serviceBound from the subclass, it'll always reply false. I've tried putting it static and non-static.</p> <p>I feel as if the singleton-pattern with getting a static instance of the service might be a (nicer) way to query the DB, but then again, there's probably a reason why Google came up with binding.</p> <p>What would be the best (and cleanest) way to have access to the db from my abstract superclass and its various subclasses?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload