PreferenceActivity Basics

SDK Version: 
The PreferenceActivity class is a very easy way to create a standard looking settings screen for any application.

Minimal coding is required. The values are stored in SharedPreferences automatically by the PreferenceActivity.

The list of settings, default values, keys to stored under are defined in a very simple xml file.

The xml file should be located in res/xml/ directory and look like something like this:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <PreferenceScreen xmlns:android=""
  3.         android:title="Settings">
  5.         <EditTextPreference android:title="Name"
  6.                 android:inputType="text" android:summary="Set your login name here"
  7.                 android:key="name" android:persistent="true" android:defaultValue=""></EditTextPreference>
  9.         <EditTextPreference android:title="Password"

Removing an app icon from launcher

SDK Version: 

Creating an application that does not appear among the launchable applications with an icon is easy.
Just do not put a launcher activity into AndroidManifest.xml

  1. <intent-filter>
  2.   <action android:name="android.intent.action.MAIN" />
  3.   <category android:name="android.intent.category.LAUNCHER" />
  4. </intent-filter>

Removing an application icon after installation programatically is a bit more tricky.
You can not disable the icon itself, but you can disable one component of an application. So disabling the applications launcher activity will result its icon to be removed from launcher.

The code to do this is simple:

  1. ComponentName componentToDisable =
  2.   new ComponentName("com.helloandroid.apptodisable",
  3.   "com.helloandroid.apptodisable.LauncherActivity");
  5. getPackageManager().setComponentEnabledSetting(
  6.   componentToDisable,

Communicating between an activity and the browser - callback

SDK Version: 

A few days ago Gabor made an article about communicating between activites. I'm currently working on a pet project that uses Oauth with the google data api, where I had to get a response from the browser, so let's take a look at communicating between an activity and a browser.

Communicating between running activities

SDK Version: 

Starting a new activity from another and passing some data to it is a simple and basic thing in android. But if you want an already running activity to come to foreground, and pass data to it, it can be a bit tricky.

First of all by default if you call an activity with an intent, a new istance of that activity will be created and displayed, even if another instance is already running. To avoid this the activity must be flagged that, it should not be instantiated multiple times. To achieve this we will set the launchMode of the activity to singleTask in the AndroidManifest.xml

  1. <activity android:name="Activity1" android:launchMode="singleTask" android:label="@string/app_name">

This way when we call this activity using an intent, if there is an existing instance, the system will route the request to it. Hoever the onCreate method, where we usually process the passed extraData, will not run this time.

How to use static variables in activities

SDK Version: 

As previously described in Leaving an Android application, when you exit an app by pressing back button its resources are not completely destroyed immediately.

I would like to explain a concrete mistake I met multiple times, in connection with this behavior, which is easy to commit, if you forget this.

Syndicate content