Code Snippet – How to get the count of rows from SQLite Database In Android?

Android comes with a utility class called DatabaseUtils which has a method queryNumEntries that can take your database object and table name as parameter and return the total number of records on that database. Quite a clean way. You don’t need to be writing ‘Select Count(*) from mytable‘ kind of stuffs.

public long count() {
    return DatabaseUtils.queryNumEntries(db,'myTablename');

Diagram of Android Activity Lifecycle (High Resolution) for quick reference

Here is a high resolution diagram (click on the image to open large version) that shows the life cycle of android activity.

It particularly shows how the following Android Activity methods are called during the life cycle of the particular activity.

  1. onCreate()
  2. onStart()
  3. onResume()
  4. onPause()
  5. onRestart()
  6. onStop()
  7. onRestart()