Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7489675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:21:05+00:00 2026-05-29T15:21:05+00:00

So I want to have one database instance for all application activities. I found

  • 0

So I want to have one database instance for all application activities.
I found the following code:

public class MyApplication extends Application {

    private static SQLiteDatabase mDB = null;

    @Override
    public void onCreate() {
        super.onCreate();
    DataBaseOpenHelper m_OpenHelper = new DataBaseOpenHelper( this );
    mDB = m_OpenHelper.getWritableDatabase();
    }

    public static SQLiteDatabase getDB() {
        return mDB;
    }
}

I don`t understand when I can close SQLiteDatabase instance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:21:06+00:00Added an answer on May 29, 2026 at 3:21 pm

    This was an issue for me when I first started out with Android, as there aren’t many tutorials on the web that describe how to correctly allow access to your database across the entire application (don’t ask me why). Here’s some sample code that exhibits three possible approaches.

    Approach #1: subclassing `Application`

    If you know your application won’t be very complicated (i.e. if you know you’ll only end up having one subclass of Application), then you can create a subclass of Application and have your main Activity extend it. This ensures that one instance of the database is running throughout the Application’s entire life cycle.

    public class MainApplication extends Application {
    
        /**
         * see NotePad tutorial for an example implementation of DataDbAdapter
         */
        private static DataDbAdapter mDbHelper;
    
        /**
         * create the database helper when the application is launched 
         */
        @Override
        public void onCreate() {
            mDbHelper = new DataDbAdapter(this);
            mDbHelper.open();
        }
    
        /** 
         * close the database helper when the application terminates.
         */
        @Override
        public void onTerminate() {
            mDbHelper.close();
            mDbHelper = null;
        }
    
        public static DataDbAdapter getDatabaseHelper() {
            return mDbHelper;
        }
    }
    

    Approach #2: have `SQLiteOpenHelper` be a static data member

    This isn’t the complete implementation, but it should give you a good idea on how to go about designing the DatabaseHelper class correctly. The static factory method ensures that there exists only one DatabaseHelper instance at any time.

    /**
     * create custom DatabaseHelper class that extends SQLiteOpenHelper
     */
    public class DatabaseHelper extends SQLiteOpenHelper { 
        private static DatabaseHelper mInstance = null;
    
        private static final String DATABASE_NAME = "databaseName";
        private static final String DATABASE_TABLE = "tableName";
        private static final int DATABASE_VERSION = 1;
    
        private Context mCxt;
    
        public static DatabaseHelper getInstance(Context ctx) {
            /** 
             * use the application context as suggested by CommonsWare.
             * this will ensure that you dont accidentally leak an Activitys
             * context (see this article for more information: 
             * http://developer.android.com/resources/articles/avoiding-memory-leaks.html)
             */
            if (mInstance == null) {
                mInstance = new DatabaseHelper(ctx.getApplicationContext());
            }
            return mInstance;
        }
    
        /**
         * constructor should be private to prevent direct instantiation.
         * make call to static factory method "getInstance()" instead.
         */
        private DatabaseHelper(Context ctx) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            this.mCtx = ctx;
        }
    }
    

    Approach #3: abstract the SQLite database with a `ContentProvider`

    This is the approach I would suggest. For one, the new LoaderManager class relies heavily on ContentProviders, so if you want an Activity or Fragment to implement LoaderManager.LoaderCallbacks<Cursor> (which I suggest you take advantage of, it is magical!), you’ll need to implement a ContentProvider for your application. Further, you don’t need to worry about making a Singleton database helper with ContentProviders. Simply call getContentResolver() from the Activity and the system will take care of everything for you (in other words, there is no need for designing a Singleton pattern to prevent multiple instances from being created).

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class as part of an asp.net application. public sealed class
I have a list of e-mails from one database which I want to check
I want to have a one-click excel export feature for my application. I therefore
I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements
I have one SQL Server Express instance with a pretty normal well formed database.
I have two sites with different SITE_IDs, but I want to have only one
I have one list that I want to take a slice of, reverse that
i have one domain link text i want to know that does google crawl
I have one very general object that I want to map to a destination
I have one small online sale business but I want to make it scalable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.