Good The topic seems to be erased, but still. Studied this topic Singleton in Android - evil? Made a class to work with the database (normal singleton):

public class DBHelper extends SQLiteOpenHelper { private static final String TAG = DBHelper.class.getName(); private static DBHelper mDbInstance = null; private static final String DATABASE_NAME = "tvbusplayer"; private static final int DATABASE_VERSION = 1; public static synchronized DBHelper getInstance(Context context) { if (mDbInstance == null) { mDbInstance = new DBHelper(context.getApplicationContext()); } return mDbInstance; } @Override public synchronized void close() { super.close(); mDbInstance.close(); } private DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); }} 

The first call to getInstance is done from MainActivity, and, following the response from the topic by reference, I pass the context of the application:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDb = DBHelper.getInstance(getApplicationContext()); 

Do I understand correctly:

  1. There are no implicit references to activity
  2. thanks to getApplicationContext (), will the instance live until the application dies?
  3. If I used getApplicationContext (), I do not need to create a private variable in the descendant of the Application class?

    0