The Activity Lifecycle

Round and round we go:The Activity Lifecycle

Android activity lifecycle tutorialEach app runs in its own process and contains one or more activities.

The Android run time manages these processes and decides which app to kill if it needs resources.

An app’s priority helps determine if the run time will kill it and its running activities.

The app’s priority is influenced by its highest priority activity.

If the Android’s memory manager needs to free up memory, it looks at the activity stack to determine the priority of the activities and which ones can be closed.

Let someone else manage your downloads!

The DownloadManager

Android DownloadManager tutorial for managing downloadsThe download manager was introduced in Android 2.3 (API level 9).

It’s a System Service that optimises the handling of long-running downloads in the background.

The download manager handles HTTP connections, monitors connectivity changes, reboots, and ensures each download completes successfully

Its good practice to use the download manager for most downloads, especially if the download continues between user sessions or if success is important.

AsyncTasks do it in the background

The golden thread

asyncTask tutorial icon

All Android components (like activities and services) start on the main thread. This is the thread that the user interacts with.

You need to keep this thread clear so that the app does not hang. It’s a good idea to put all time-consuming processes on a separate thread.

Put file operations, network lookups, database interaction, etc. on a background thread.

 

Doing it on the quiet: how to use an IntentService

intentService iconYou should do time consuming work, like downloading files, on a separate thread.

This will free up the main thread so your user can carry on using the app.

IntentServices are the preferred way for doing long running background operations.

You can request an IntentService from any of your fragments or activities. The requests are queued and handled one-after-the-other. The IntentService stops once all the requests have been handled.

Start an activity for a result

Start me up: Starting an activity to get a result

Starting activity for result - result iconSometimes, you may need to get data from another activity. That activity could be in your app or in another app.

You could use startActivityForResult() to get that data for you.

Exchanging primitive data between activities

dataXchange iconYou may want to pass data from one activity to another.

It all depends on the type of data

You could put the data in a central point and then access it from anywhere.

Here are some of the ways to do that:

Using a Loader to get your data out of a database

So what’s a Loader?

loader iconWell, it's the next best thing since sliced bread! This is what they can do for you:

  • You can use loaders in activities and fragments
  • Loaders have been available since Honeycomb (API level 11) and also since Donut (API level 4) if you use the support library
  • Loaders can be used to load any kind of data from any data source (for example, Arrays and Databases)
  • Loaders work on a separate thread so your app carries on working while the Loader gets the data
  • Loaders monitor the data source for any changes and updates the data it gives you
  • Loaders take care of restoring the cursor after a configuration change without having to do a re-query

Accessing the database from another app

Query app iconYou use a content provider to make your database accessible to other apps.

You use a content resolver to access a database in another app.

Using the Content Provider

 

Using the content provider iconWe covered building our provider class in Part 1. Now in Part 2, we’ll see how we use the provider to access the database.

We will be accessing the database from within the app that created it but we will still use a content resolver to do so.

Building the Content Provider

This is part 1 of creating our custom content provider. This is where we build our ContentProvider class.

Building iconIn part 2, we'll show how we use the provider within the same app to access the database.