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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:15:13+00:00 2026-06-12T02:15:13+00:00

I need a suggestions how to achieve something in android and Java. I have

  • 0

I need a suggestions how to achieve something in android and Java. I have a big project which has more than 50 activities and I use two different database classes to query sqlite statements and retrieve information from my system and user database. Here is an example how I am using and initializing my database :

SystemDatabaseHelper dbHelper = new SystemDatabaseHelper(this, null, 1);
dbHelper.initialize(this);

I am doing that in activity and the last few days I read a lot for memory leaks in android and the whole information about giving Context to a non-activity classes and the leaks which this can cause. My question is which is the best way to create some class and initialize it only from main activity and than use it in all other activities without initializing it again and again.

Any suggestions which is the best way to achieve this…i have some ideas,but want to hear your suggestions and best practices.

  • 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-06-12T02:15:14+00:00Added an answer on June 12, 2026 at 2:15 am

    if you are trying to initialize your database helper class only once you’re looking for singleton right?? here is an example how you can make it

    public class ContactDBHelper extends SQLiteOpenHelper {
    
        private ContactDBHelper(Context context, String name,
                CursorFactory factory, int version) {
            super(context, name, factory, version);
    
        }
    
        private static ContactDBHelper mInstance;
    
        public static synchronized ContactDBHelper getInstance() {
    
            if (mInstance == null) {
                mInstance = new ContactDBHelper(Util.getApplicationContext(),
                        ContactDB.DB_NAME, null, ContactDB.DB_VERSION);
            }
            return mInstance;
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(ContactDB.Contact.CREATE_STMT);
            db.execSQL(ContactDB.Contact.CREATE_PHONE_NUMBER_INDEX);
            db.execSQL(ContactDB.Contact.CREATE_REVERSE_PHONE_NUMBER_INDEX);
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        protected Object clone() throws CloneNotSupportedException {
    
            throw new CloneNotSupportedException();
        }
    
    }
    

    if you call ContactDBHelper.getInstance(); the first time , it will initialize the instance , after that any application component or any method will call it , it wont initialize it will return the singleton initialized instance .
    note : for the Util.getApplicationContext it is a static helper method that returns application context which is set in the main Activity .

    for memory leaks , you can avoid or protect your app from it using WeakReference or SoftRerference

    here is the Util class

    public class Util {
    
        private static WeakReference<Context> applicationContext;
    
        public static Context getApplicationContext() {
            return applicationContext.get();
        }
    
        public static void setApplicationContext(Context context) {
            applicationContext = new WeakReference<Context>(context);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a java application which can merge docx files. Any suggestions?
I have something cool in mind for a new personal project, more to update
I need suggestions on how can I download attachments from my IMAP mails which
I need suggestion about YAMI library . I have a system which receives Json
I'm looking for a DVCS which would allow me to use something like a
I'm working on application and I need some suggestions which is the best way
I have asked something similar before, but never go to the solution I need.
Need suggestions on implementing associating single or many objects to an entity. All soccer
I need some asp.net pagination code samples. I would like suggestions on open source
I'm just looking for suggestions on the best way to do this... I need

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.