1
00:00:05,380 --> 00:00:09,060
So, so far we've used a very trivial example of accessing data,

2
00:00:09,200 --> 00:00:12,480
where we open the database, grab the data and then close it again.

3
00:00:12,830 --> 00:00:17,260
What do we do in the situation of needing more complex data retrieval?

4
00:00:17,300 --> 00:00:24,500
Let's go through and look at other ways that we can access data in our apps. So firstly, the problem

5
00:00:24,500 --> 00:00:30,500
might be that we're accessing a database when the user re-orientates their device, say from portrait

6
00:00:30,500 --> 00:00:31,350
to landscape,

7
00:00:31,490 --> 00:00:33,990
or they might press Home and access another app.

8
00:00:34,160 --> 00:00:39,050
Potentially we could have corrupted data because the Lifecycle doesn't have any idea about the database

9
00:00:39,230 --> 00:00:42,450
being accessed.

10
00:00:42,520 --> 00:00:48,600
So what we can do is we can effectively open our database once, and then query or re-query the data as needed,

11
00:00:48,640 --> 00:00:52,080
and only close the database when the Activity is destroyed.

12
00:00:52,110 --> 00:00:56,920
So in other words, we want the Lifecycle to be aware of the database. Then we can do things like binding to

13
00:00:56,920 --> 00:00:58,530
a view on the screen, for example

14
00:01:01,430 --> 00:01:02,810
We can also do it manually.

15
00:01:02,890 --> 00:01:07,780
So we can add code to each life cycle event to do the relevant thing for our database,

16
00:01:07,780 --> 00:01:12,310
for example, close it when the activity is closed etc. And it means that we're adding a lot of code to

17
00:01:12,310 --> 00:01:14,470
each screen in our app by doing it that way.

18
00:01:16,940 --> 00:01:21,300
AsyncTask is another way, so we can write some code to use these tasks any time we want to access

19
00:01:21,300 --> 00:01:26,380
the database, but that ends up being a lot of code.

20
00:01:26,440 --> 00:01:28,570
So that brings us to Content Provider.

21
00:01:28,640 --> 00:01:33,230
Now this is actually a standard way to provide access to a structured set of data -

22
00:01:33,410 --> 00:01:35,390
sqlite's a good example of this.

23
00:01:35,420 --> 00:01:39,360
So Content Providers are a core feature of Android Development.

24
00:01:40,800 --> 00:01:46,080
So we set up a Content Provider in our app, and then we can provide a mechanism to return the results

25
00:01:46,080 --> 00:01:47,450
to the calling process -

26
00:01:47,610 --> 00:01:49,000
our app, or any other app

27
00:01:49,000 --> 00:01:51,980
if we choose to share the provider's data.

28
00:01:51,980 --> 00:01:54,390
Now we don't technically need a Content Provider

29
00:01:54,570 --> 00:01:58,190
if you don't intend to share data, but I feel it's better to do it this way

30
00:01:58,200 --> 00:02:03,570
so your app is set up the right way in the event that you do decide to share the data in the future.

31
00:02:05,870 --> 00:02:12,170
Now Android provides us with a good list of built in Content providers for audio, video, images etc. and

32
00:02:12,170 --> 00:02:14,600
we will work more on those in future videos.

33
00:02:16,820 --> 00:02:22,730
My recommendation is to use a Content Provider for all your database projects because it adds more flexibility,

34
00:02:23,060 --> 00:02:27,430
and when it comes down to it, it's not much, if any, more complex than other methods.

35
00:02:30,790 --> 00:02:35,570
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

36
00:02:35,570 --> 00:02:42,470
Provider and use a Cursor Loader to allow us to maintain our contacts database - add your contacts, edit and/or

37
00:02:42,470 --> 00:02:45,350
delete existing contacts, as well as displaying them.

38
00:02:45,350 --> 00:02:47,560
So let's start work on that in the next video.

