Android docs on Context.
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
So when you create a View you need to pass Context to it, but why? What your View will loose if it is not passed Context. Your View will loose the ability to access system resources. For eg suppose there is a Drawable that is to be set as background of your View, without context your View cannot access that Drawable.
public class MyView extends View { public MyView(Context context) { super(context); Drawable d = context.getDrawable(R.drawable.ic_launcher_background); getDrawable(R.drawable.ic_launcher_background); //without context IDE will give error cannot resolve getDrawable(int). } }
So when you create a View you pass Activity Context to it, because your View is tied to your Activity. It should not outlive Activity.
MyView myView = new MyView(this);
You can use this as Context because your Activity extends from ContextThemeWrapper, which extends from ContextWrapper which extends from Context.
Now if you want to create a Database, and if your app has multiple Activities which need access to DB, then your DB should outlive the Activity which created DB instance, so in this case you should use Application Context to create DB.
Even if you have a single Activity app, then also your DB should outlive your Activity, because your Activity will be restarted on every Rotation. But your Application is not. So here also you should pass Application Context.
As a general rule, think of Context as the lifetime for your newly created object, For DB you generally want Application lifetime.
To make it clear consider an example : Your app starts with Activity A, which download large Bitmap form network, creates a DB instance on its Context and saves Bitmap to the DB. After that your Activity A starts Activity B and A is finished. Now your Activity A is no longer required and should be garbage collected. But your DB is still alive on its Context so A will not be gc'ed and will result in Memory Leak.
Finally as Naresh as already pointed in his answer you should make your DB a Singleton.
Database db = new Database(MainActivity.this);. It should be an Activity context not a Class context.