Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Sugar ORM when also using singleton application class

Tags:

orm

I have extended the Android.Application class so that I can keep state (using the application as the singleton).

However, I now want to use the Sugar ORM library to simplify my DB access, but the Sugar docs (http://satyan.github.io/sugar/getting-started.html) require that I specify 'SugarApp' as my application class in AndroidManifest.xml.

Which clashes with my Application class that extends Android Application.

Is there an alternative approach?

From the Sugar docs:

E.g. by changing the android:name attribute of the application tag.

<application android:label="@string/app_name" android:icon="@drawable/icon"
android:name="com.orm.SugarApp">
.
.
<meta-data android:name="DATABASE" android:value="sugar_example.db" />
<meta-data android:name="VERSION" android:value="2" />
<meta-data android:name="QUERY_LOG" android:value="true" />
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example" />
.
.
</application>
like image 655
DEzra Avatar asked Feb 21 '14 15:02

DEzra


2 Answers

Answering my own question -tut tut ;-)

It's simple, I just changed my Custom App class to extend SugarApp instead of Android.Application, as SugarApp itself extends android app!

like image 92
DEzra Avatar answered Nov 06 '22 14:11

DEzra


If you take a look at SugarApp class, you will find that it extends Application class overriding the onCreate() & onTerminate() methods doing its scaffolding:

@Override
public void onCreate() {
    super.onCreate();
    SugarContext.init(this);
}

@Override
public void onTerminate() {
    super.onTerminate();
    SugarContext.terminate();
}

The only thing you have to do if you want to keep your base application class is to add those method calls to your custom class.

In this way, you can maintain your custom application class instead of extending SugarApp class.

like image 42
Victor de Francisco Domingo Avatar answered Nov 06 '22 14:11

Victor de Francisco Domingo