when I made an SQLite database for an Android app I made a helper class that does not extend any other class. it is just to set up the database, in this example it is called PlayGame. it has a private class inside it shown here:
private static class DbHelper extends SQLiteOpenHelper
when I use the database for any reason in the main UI class called SQLiteExample I have to create an instance every time like this,
PlayGame entry = new PlayGame(SQLiteExample.this);
entry.open();
entry.createEntry(name, hits);
entry.close();
I am creating many instances of this database class called PlayGame in the other Activity class. Like in most all of my methods have to make instances to do the required function like read information into or out of the database.
so I read in best practices of android documentation that creating instances is heavy on memory and is best avoided. If this is the case is there a better way to do this? and does my example look like a bad use of memory?
Use singleton for every instance you want to use. instead of create a new instance of any object you will use the same object.
http://en.wikipedia.org/wiki/Singleton_pattern
Related
What is the use of this private constructor and why we instantiate it's object under the class.
DatabaseHelper._privateConstructor();
static final DatabaseHelper intance =DatabaseHelper._privateConstructor();
In general, we'll use a private constructor when we don't want things to be instantiated more than once. This lets us implement the singleton pattern:
https://www.geeksforgeeks.org/singleton-design-pattern/
In this case, we don't want multiple instance of the database. So, it'll instantiate the database if it hasn't already been instantiated, or it'll return the existing instance of the database if it has been instantiated previously.
It is a design pattern called the singleton pattern.
It's main purpose is to ensure that only one instance of that class ever exists in the application.
To ensure this purpose, the pattern implements techniques that have serious disadvantages. People are overusing it, sometimes using it as a justification as to why they have those disadvantages in their program that would otherwise be considered bad design. It has become an anti-pattern. See Why is Singleton considered an anti-pattern?.
In addition to given answers, it is Eager singleton, therefore once the class is initialized, the instance is also initialized, even the instance is not used. However still it has been instantiated only once.
See also Singleton lazy vs eager instantiation
As the other answers state, it's the singleton pattern. Here's a link that explains it more.
I have three classes,
Activity - displays the listview with my single_list_item.xml
SQLiteOpenHelper - I'm currently running my queries here.
My activities and my SQLiteOpenHelper talks together well. However I'm not sure how to run a query from my third class
BaseAdapter - This one is not in my manifest, and with my current skills I can't access the db from this one - as I normally do from activity classes.
I want to hide a textview in my single_list_item.xml depending on the value from my query(the query only returns 0 or 1). I can easily hide it or show it statically by defining tv.setVisibility(View.GONE) etc. - But I want this one to be active or not depending of the usersettings.
Any help is greatly appreciated.
A common approach is to create a singleton, that is first initialized with a Context. That holds the SQLiteDatabase, and has all the data specific methods that you use from your application.
Thus, from your BaseAdapter, you call MyDBSingleton.getInstance().getData()
Be sure, that the singleton class keeps an ApplicationContext, and not a reference to an Activity/Service, as that could cause leaks...
I have one Activity, I created class called DatabaseHelper that extends SQLiteOpenHelper,
now when I want to use it (has I see in many simple examples) I need to have new SQLiteDatabase Object & implement insert, update, delete... in my Activity code.
What is the right design if I want to work through database with number of activities and to create DatabaseHelper just once, for all the activities will work with it.(and not have duplicate code.
simply: what is the right classes SQLite structure for number of Activities
Thank you!!
Just use one DatabaseHelper throughout your application, there's no profit in repeating yourself.
Make your DatabaseHelper a singleton across your entire application's lifecycle. Check out my blog post on the topic:
Correctly Managing Your SQLite Database
I have a class named PatientDetails in which i am storing the values from the xml and then need to access its variables and method from the service as well from the activity at the same point of time ??
This is a typical multi thread scenario. You can do it without any troubles as long as you are just reading the data.
If you are reading the data from patient details class through your activity and writing data to it through your service you will get into run time exceptions. You have carefully synchronize the variables or methods in such cases.
One way to share a 'helper' class is to hold a 'static' reference to a single instance of it in the Application component of your app. Example...
public class MyApp extends Application {
public static detailsHelper;
#Override
public void onCreate() {
detailsHelper = new PatientDetails();
}
}
When you need to use the 'helper' in any other component such as an Activity or Service you simply reference it by the Application name as follows...
MyApp.detailsHelper.doSomething();
Technically speaking, under default conditions there is no such occurrence of two components accessing something at the same time because an Android Application and all of its components exist within a single process with a single thread of execution.
You should be very careful, however, if any of the components execute code which uses threads. For example, an Activity using an AsyncTask or perhaps using an IntentService which creates its own worker thread to do work. In this case, make sure any methods in the 'helper' class which write data, do so in a thread-safe manner.
What's the best way to access the database through an async task?
I don't think I should pass in a reference to the DbAdapter that the activity is using (could be closed as the activity may be garbage-collected).
Also, the db needs a context to be opened and closed, but I don't have that with the async task.
AsyncTask is an abstract class, so you have to extend it.
Create a class which extends AsyncTask, then in the constructor of this class you can add every things you want like the Context.