Skip to content

Instantly share code, notes, and snippets.

@getsadzeg
Last active May 3, 2019 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getsadzeg/c3742f14f0d33ef08a8d4bbbd500fd6c to your computer and use it in GitHub Desktop.
Save getsadzeg/c3742f14f0d33ef08a8d4bbbd500fd6c to your computer and use it in GitHub Desktop.
Content Providers - A layer between the database and Activity

Yes. Another middleman!

UI code-> (Content Resolver) -> Content Provider -> Database

Content Providers are (seemingly) good abstraction layer between data source and UI. Some advantages of this include data validation.

They enable you to decouple your application layers from the underlying data layers, making your application data-source agnostic by abstracting the underlying data source.

Content URIs

content_authority(usually package name) and content_name(our Java class for Provider) is written in Android Manifest.

content://content_authority/table_name/row_id

Note: row_id should be specified when we want a single row, of course.

Now in Contract class, we should make those constants: String CONTENT_AUTHORITY, Uri BASE_CONTENT_URI, String PATH, and in Entry subclass, Uri CONTENT_URI; Use Uri's withAppendedPath(Uri baseUri, String path) to append base URI and path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment