0

I constructed an SQL Lite handler class which extends SQLiteOpenHelper:

public class DatabaseHandler extends SQLiteOpenHelper

The class have this constructor:

public DatabaseHandler(Context context) 
{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

How can i use this class from multiple activites? that is throuhout my entire app?

When i try to instantiate the class from another activity it gives an error because the context is wrong.

What is the best practice for excessing a db from multiple activities?

Michael A
  • 5,770
  • 16
  • 75
  • 127
  • possible duplicate of [Do I need to call getWritableDatabase() everytime I manipulate data](http://stackoverflow.com/questions/18595482/do-i-need-to-call-getwritabledatabase-everytime-i-manipulate-data) – Jon Adams Dec 17 '13 at 05:47

4 Answers4

0

Each activity should have its own instance of SQLiteDatabase.

See Do I need to call getWritableDatabase() everytime I manipulate data for more discussion on the subject, including example code.

Community
  • 1
  • 1
Jon Adams
  • 24,464
  • 18
  • 82
  • 120
  • How do i intantiate SQLiteDatabase,SQLiteOpenHelper on the diffrent activities? this wont give me a null pointer exception? SQLiteOpenHelper helper; SQLiteDatabase data = helper.getWritableDatabase(); – Michael A Dec 17 '13 at 06:19
  • Create a new instance of your helper, then call the getWritableDatabase() method. Optionally, pass context to a static method you create in the helper class you create that also implements a factory pattern. – Jon Adams Dec 17 '13 at 06:27
0

Take a global static

Utils.class

public static DatabaseHandler data;

SplashActivity.class

Utils.data=new PortfolioDatabaseManager(getBaseContext());

You can use it as per your need in any class by accessing like: Utils.data

You can add getReadDatabase & getWriteDatabase in database class:

public SQLiteDatabase getReadDatabase(Context context){

        // create or open the database
                DatabaseHandler helper = new DatabaseHandler(context);
                SQLiteDatabase data = helper.getReadableDatabase();
        return data;

    }

public SQLiteDatabase getWriteDatabase(Context context){

        // create or open the database
                DatabaseHandler helper = new DatabaseHandler(context);
                SQLiteDatabase data = helper.getWritableDatabase();
        return data;

    }
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
0

You can extend the class with the Application class and after that you can use all the members and functions of that class from any of the activity. You can make your class a member of the Global class and use it in whole application.

Like this :

class Global extends Application 
{
    ........
    ........
}
  • Technically you can do this. But per the "best practices" the op requests, it is better to instead get an instance from the db helper class for each new context (activity). – Jon Adams Dec 17 '13 at 06:30
0

When managing persistent data, the best practice in OOP is the use of the "Gateway Pattern".

Gateways encapsulate and centralizes the application database for easier access.

public class OfflineGateway(){
  private static OfflineGateway instance;
  private Activity activity;

  private OfflineGateway(){
    //some initialization
  }

  public static OfflineGateway getInstance(Activity activity){
    if(instance == null)
      instance = new OfflineGateway();

    return instance;
  }

  //private accessible methods
  private String getStringData(){
    //do something here
  }

  private void setStringData(String string){
    //do something here
  }
}
Rick Royd Aban
  • 904
  • 6
  • 33