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 6763239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:27:09+00:00 2026-05-26T14:27:09+00:00

I have a DB that I use in all my activities. There is only

  • 0

I have a DB that I use in all my activities. There is only one record in the DB.

In the first activity it is opened or created and then put in my globally used object like this

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // first get the current data from the DB
        myDBAdapter = new MyDBAdapter(this);
        GlobalVars.myDBAdapter = myDBAdapter; // we store the DBAdapter in our global var

        myDBAdapter.open();

        Cursor cursor = myDBAdapter.fetchMainEntry();
        startManagingCursor(cursor);
        // if there is no DB yet, lets just create one with default data
        if (cursor.getCount() == 0) {
            createData();
            cursor = myDBAdapter.fetchMainEntry();
            startManagingCursor(cursor);
        }

Now in another activity I access the already open DB like this…

GlobalVars.myDBAdapter.updateMainEntry(1,.....);

I do not close the DB when leavin one activity to go to the next. The DB is just accessed (since it has been opened at the very first activity).

Only when leaving the app I clode the DB like this…

@Override
    protected void onDestroy() {
        super.onPause();
        myDBAdapter.close();

    }

The background why I am also asking this is I get this error…

Finalizing cursor android.database.sqlite.SQLiteCursor@48106730 on
mainEntry that has not been deactivated or closed

and it seems that my app crashes on certain devices – but I can’t find the reason for it during debugging.

Is that correct and best practice, or do I have to close the DB when I leave the activity and open it when entering the next activity when switching between activities?

Many thanks!

  • 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-26T14:27:09+00:00Added an answer on May 26, 2026 at 2:27 pm

    The best thing (I have it tested in a few apps of mine) is to:

    • declare database adapter as an activity’s instance variable:

      private DBAdapter mDb;

    • create it and open in activity’s onCreate():

      mDb = new DBAdapter(this);
      mDb.open();

    • close it in activity’s onDestroy():

      mDb.close();
      mDb = null;

    Works like charm.
    A side note: the Application class onTerminate “will never be called on a production Android device” according to the docs.

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

Sidebar

Related Questions

I have created a custom theme for my activities that they all use. In
I have a few iFrames that all use a single background image. Whenever I
We have a Library Project that we use for all our central reused code
I have custom event that has several different subscribers who will all use the
In php I have a script, that I use mod_rewrite to redirect all calls
All, I have my application setup so that i can use a specific username
I have defined CSS in MasterPage so that all pages can use the same
In one of my Activities I do have up to six different AsyncTasks that
I have two activities A and B. B : is a setting activity that
I have clients that use our web site (asp.net) that they have to be

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.