The services in Android

A service is a task running in the background to perform actions without a user interface and they are used to perform long running operations with a significant use of resources by avoiding delays in the user experience.
The services belong to one or both of the following categories:

  • started: the service is launched using the the method startService()
  • bound: the service is launched using the the method bindService() and the components of the application that has launched the service can communicate with the same service

Continue reading “The services in Android”

Passing an object from an Activity to another

The method startActivity(Intent intent) of the class Activity allows you to call a second activity specified using the argument Intent.
You can associate primitive data or primitive data array to the argument Intent and then the second Activity can access them, you can also pass objects of type String using methods of the class Bundle like put*().
In this post I show an example to pass a more complex object between an Activity and another.
Continue reading “Passing an object from an Activity to another”

Preference element in xml and the click event

If you have an EditTextPreference tag in a xml file you can catch the click event implementing OnSharedPreferenceChangeListener but it doesn’t work if you have a Preference tag in the xml file.

Consider the following code inside a xml file for the preferences:

Continue reading “Preference element in xml and the click event”

Getting the Context inside a Fragment

It is very easy to get the Context inside an Activity:

  • with the getApplicationContext() method
  • using only “this” because the Activity class extends the Context class

But you can’t use any of these two methods inside a Fragment and you have to replace them respectively with:

  • getActivity().getApplicationContext()
  • getActivity()

I often do this replacement when I use code written for an Activity, but that I must use in a Fragment.