Reputation: 207
DatabaseHelper class's onCreate method is called when db.getWritableDatabase() or db.getReadableDatabase() is called. So, whenever use the dbhelper for the first time, onCreate will be called at that time.
I want my database to be called whenever the app is started for the first time instead of first use of dbhelper.
Upvotes: 0
Views: 1035
Reputation: 544
Initialize your Db Class in applicaion Class ....
public class AppController extends Application {
private static AppController mInstance;
private DatabaseManager databaseManager;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
databaseManager = new DataBaseManger(mInstance);
}
public static synchronized AppController getInstance() {
return mInstance;
}
}
Declare the Application Class in Android Manifest...
<application
android:name=".app.AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
---------------------------------------
----------------------------------------
-------------------------------------------
</application>
Upvotes: 1
Reputation: 6107
I want my database to be called whenever the app is started for the first time instead of first use of dbhelper.
Create a Custom Application class and Initialize your database in Application's onCreate()
method. This will ensure your database will be initialized before any Activity
launch. Because Application class run before any Activity launch.
public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
// Initialize your Database here
}
}
Define you Application Class in AndroidManifest.xml
<application
android:name="com.example.MyApplication"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/BaseTheme">
...................
...................
</application>
Upvotes: 0