Doing it in the background. Services Tutorials. Part 1: A Simple Service

simple service tut icon

Our tutorial app shows you how to use Services to:

  • use a simple Service to update a user’s score on a server
  • use a foreground Service to play music in the background

There are two parts to the tutorial: Part 1 shows you how to use a simple Service and Part 2 shows you how to use a foreground Service.

All about Services

At your Service: Using Services

Services icon

Services are app components that you can use to do work in the background. They don’t have a User Interface.

They are started and stopped by other components (like Activities, Broadcast Receivers and other Services).

You can also share Services with other apps.

Using Threads Tutorial

Forget the grass, get off the Main thread!

threads tutorials icon

Have you read the article on Processes and Threads yet?

Always remember the two most important rules when working with threads:

  • Don’t block the Main thread
  • Don’t try and access the UI directly from the worker thread

Speed up your apps response times. Move all processing and I/O operations off the main thread. Do the work in a child thread. Our tutorial will show you how.

Processes and Threads

Android processes and threads tutorial

All Android apps run in their own process and by default, in a single thread.

Processes

It’s about multitasking

Android follows a multitasking design. This allows a number of applications to run at the same time. The problem is that all these apps need memory which is in short supply.

The parent's in control

fragments tut part3 icon

We only have one activity in our app. It hosts the fragments.

If you haven't already done so, have a look at :

We’re using the support library so make sure that you import the correct class:

fragment tut part2 icon

We'll build the two fragments in this part of the tutorial.

If you haven't already done so, have a look at :


fragmentsTut part1 icon

Our tutorial app has one activity and two fragments. One fragment contains a list and the other an image.

Running the app on a small device in portrait mode, displays the list fragment. Selecting an item in the list displays the second fragment containing an image.

Flipping the device to landscape displays both fragments side by side.

Running the app on a tablet displays the fragments side by side in both portrait and landscape mode.

Fragments: What you should know

Seriously, what are fragments?

fragments iconThink of components like buttons and images. You can reuse them over and over in different activities. You can also rearrange them within an activity.

Fragments are similar.

Fragments let you divide your activity into reusable components which have their own User Interface and lifecycle.

How to save your Activity’s Instance State

What is Instance State?

ConfigChange iconThe data that is saved by the system to restore the previous state of an activity is called the Instance State. This data is saved as a collection of key/value pairs in a Bundle object.

Same as it ever was: Persisting the Activity state

ActivityState iconWhen an activity is paused or stopped, its state is kept in memory. When the activity resumes, it is restored from memory.

When the activity is destroyed, it loses all information about its state. When the activity is restarted, it is created from scratch. It is a new activity.