Android Notepad App is simple notepad app for android, written in Java. allows you to create, edit and delete notes locally using Room Persistence Library.
- User can create a note.
- User can edit a note.
- User can delete a note.
- When closing the browser window the notes will be stored and when the User returns, the data will be retrieved.
project structure:
com.example.notepadapp
├── AppExecutors
├── MainViewModel
├── Adapters
│ └── NotesAdapter
└── Database
│ ├── AppDatabase
│ ├── Notes
│ └── NotesDao
└── UI
├── AddActivity
├── EditActivity
└── MainActivity
- Room Database.
- Executor
- View Model and Live Data.
- Parcelable.
Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.
Apps that handle non-trivial amounts of structured data can benefit greatly from persisting that data locally. The most common use case is to cache relevant pieces of data. That way, when the device cannot access the network, the user can still browse that content while they are offline. Any user-initiated content changes are then synced to the server after the device is back online.
For a guide on applying Room's capabilities to your app's data storage persistence solution, see the Room training guide. And the CodeLabs Android Room with a View.
- 7 Pro-tips for Room.
- Room Database: Getting Started.
- Android Architecture Components, Room, LiveData, and ViewModel.
An object that executes submitted Runnable tasks. It's normally used instead of explicitly creating threads for each of a set of tasks. I have added the AppExecutor class and make it singleton so we always use the same instance of the class.
ViewModel : Provides data to the UI and acts as a communication center between the Repository and the UI. Hides the backend from the UI. ViewModel instances survive device configuration changes. LiveData : A data holder class that follows the observer pattern, which means that it can be observed.
The Room persistence library supports observable queries, which return LiveData objects. Observable queries are written as part of a Database Access Object (DAO).
Room generates all the necessary code to update the LiveData object when a database is updated. The generated code runs the query asynchronously on a background thread when needed. This pattern is useful for keeping the data displayed in a UI in sync with the data stored in a database.
You can read more about Room and DAOs in the Room persistent library guide. And to understand View Model and Live Data more, check these videos: Android Jetpack: LiveData, Android Jetpack: ViewModel .
- LiveData & ViewModel — Making your own magic
- Android Studio Guide to ViewModel & LiveData (2020 Edition)
A Parcelable is the Android implementation of the Java Serializable. It assumes a certain structure and way of processing it. This way a Parcelable can be processed relatively fast, compared to the standard Java serialization.
To allow your custom object to be parsed to another component they need to implement the Parcelable interface. Like so,
public class Notes implements Parcelable { }
then implement its necessary methods.
Once you have implemented it in you object, you can put it into the extra of an intent.
intent.putExtra("notes", new Notes("1","note-title","note-body"));
and to access the parcel on the other side,
Intent intent = getIntent();
notes = intent.getParcelableExtra("notes");