So, so far we've used a very trivial example of accessing data,
where we open the database, grab the data and then close it again.
What do we do in the situation of needing more complex data retrieval?
Let's go through and look at other ways that we can access data in our apps. So firstly, the problem
might be that we're accessing a database when the user re-orientates their device, say from portrait
to landscape,
or they might press Home and access another app.
Potentially we could have corrupted data because the Lifecycle doesn't have any idea about the database
being accessed.
So what we can do is we can effectively open our database once, and then query or re-query the data as needed,
and only close the database when the Activity is destroyed.
So in other words, we want the Lifecycle to be aware of the database. Then we can do things like binding to
a view on the screen, for example
We can also do it manually.
So we can add code to each life cycle event to do the relevant thing for our database,
for example, close it when the activity is closed etc. And it means that we're adding a lot of code to
each screen in our app by doing it that way.
AsyncTask is another way, so we can write some code to use these tasks any time we want to access
the database, but that ends up being a lot of code.
So that brings us to Content Provider.
Now this is actually a standard way to provide access to a structured set of data -
sqlite's a good example of this.
So Content Providers are a core feature of Android Development.
So we set up a Content Provider in our app, and then we can provide a mechanism to return the results
to the calling process -
our app, or any other app
if we choose to share the provider's data.
Now we don't technically need a Content Provider
if you don't intend to share data, but I feel it's better to do it this way
so your app is set up the right way in the event that you do decide to share the data in the future.
Now Android provides us with a good list of built in Content providers for audio, video, images etc. and
we will work more on those in future videos.
My recommendation is to use a Content Provider for all your database projects because it adds more flexibility,
and when it comes down to it, it's not much, if any, more complex than other methods.
Alright, so I think at this point it's now time to put together an app. So the app's going to define a Content
Provider and use a Cursor Loader to allow us to maintain our contacts database - add your contacts, edit and/or
delete existing contacts, as well as displaying them.
So let's start work on that in the next video.