Sunday, February 27, 2011

Difference between pressing HOME and Back key for Android activity

After you start an activity, if HOME key is pressed, then the current activity is stopped and its task goes into the background. The system retains the state of the activity - i.e. onSaveInstanceState will be called. If the user later resumes the task by selecting the launcher icon that began the task again, the task comes to the foreground and resumes the activity at the top of the stack.

However, if BACK key is pressed, the current activity is popped from the stack and destroyed. The assmuption is the activity is done and will not be used again. So the system does not retain the activity's state - i.e. onSaveInstanceState will not be called.

Jonathan

Automatically format the java source file for Android Eclipse

Eclipse java project, Ctrl + Shift + F can be used to automatically format the source file.
You can also first select the lines in source file and then use Ctrl+I to only format the selected lines.
Another option is automatically formatting the source file when saving the file. To configure this, go to Windows->Preference->Java->Editor->Save Action, and enable the option of "Format All Lines".
This will save a lot of time to manually format the code and also make it more readable.

Other useful short cut keys:
Ctrl+Shift+E: showing the list of all opened editor,
Ctrl+Shift+F6: navigateing to previous editor
Ctrl+F6: navigating to next editor
Ctrl+/: comment and uncomment all selected lines.
Jonathan

Wednesday, February 23, 2011

Andoid Activity Life Cycle - onSaveInstanceState and onRestoreInstanceState

onSaveInstanceState and onRestoreInstanceState are two methods involved in Android Activity life cycle. There are several factors deciding when they will be called.

First, the data bundle mananged by these two methods are only valid within a single application running session. So if the application exits and restarts, then OnRestoreInstance will not be called, since no data are available. For the same reason, null is passed as parameter for onCreate method when onCreated is called in the first time.
MyActivity onCreate (null)
MyActivity onStart
MyActivity onResume

Second, onSaveInstanceState is always called before onPause method when starting to deactivate or close the Activity.
MyActivity onSaveInstanceState
MyActivity onPause

Third, in the same application session, onRestoreInstance will only be called if onDestroy has been called before it. Or in other word, onCreate is called before it.
MyActivity onSaveInstanceState
MyActivity onPause
MyActivity onStop
MyActivity onDestroy
MyActivity onCreate (obj)
MyActivity onStart
MyActivity onRestoreInstanceState
MyActivity onResume

A sample of full cycle looks as below:

MyActivity onCreate
MyActivity onStart
MyActivity onResume
MyActivity onSaveInstanceState
MyActivity onPause
MyActivity onStop
MyActivity onDestroy

MyActivity onCreate
MyActivity onStart
MyActivity onRestoreInstanceState
MyActivity onResume
MyActivity onSaveInstanceState
MyActivity onPause
MyActivity onStop
MyActivity onDestroy

Usually, onRestoreInstanceState will not be called since most of time the activity is still alive after it is stopped, as shown below:

MyActivity onCreate
MyActivity onStart
MyActivity onRestoreInstanceState
MyActivity onResume
MyActivity onSaveInstanceState
MyActivity onPause
MyActivity onStop
MyActivity onRestart
MyActivity onStart
MyActivity onResume


There are two ways to force the activity to be destroyed immediately.

The first way is changing the orientation of the simulator, it will force all activities to be destroyed and then created again. Another way is enabling Immediately destroy activities from dev tool's development settings page.

Jonathan

Tuesday, February 22, 2011

How to change the orientation on Android simulator

During the testing of event handler of onConfigurationChanged, you will need to change the simulator's orientation.

Actually, it is quite simple to do so, first setting the current active window to Android simlator, and then press the key of Ctrl+F11 on your desktop computer (not the android simulator's key pad).

Jonathan

Monday, February 21, 2011

Install Windows Phone 7 Developing Tool on Windows Server 2008

By default, only Vista and Windows 7 are supported for the Windows Phone 7 development tools. In order to install it on Windows Server 2008, the following steps are required:
1. download Windows Phone Developer tool from
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=04704acf-a63a-4f97-952c-8b51b34b00ce&displaylang=en

2. extrace the downloaded file by running a console command: vm_web.exe /x

3. Edit extracted file baseline.dat, by looking for section named [gencomp7788]
Changing the value InstallOnLHS from 1 to 0
Changing the value InstallOnWin7Server from 1 to 0

4. Save the udpated baseline.dat

5. Run setup.exe /web

6. Download and install Windows phone developer Tool Janunary 2011 Update from
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=49b9d0c5-6597-4313-912a-f0cca9c7d277&displaylang=en


Jonathan

Subclass Application class for Android project

In order to handle several system events, like low memory, configuration change, you will need to subclass from Android Application class. Note that only creating a derived Application class and adding it into the project are not enough. You also need to update the project's manifest file to specify the derived class full name in Application node's name attribute, as shown below:

MyApplication sub class:
public class MyApplication extends Application {
public static final String MyAppTag = "MyAppTag";

@Override
public final void onCreate() {
Log.v(MyAppTag, "MyAppliation onCreate");
super.onCreate();
}
}

Updated AndroidManifest.xml:

android:label="@string/app_name"
android:name="MyApplication"
android:description="@string/app_desc"


Jonathan

Sunday, February 20, 2011

Write and view debug output from Eclipse for Android

Sooner or later, you will need the debug output to help you in Android development. The old System.out.println still works, but you need to view the output from Android LogCat instead of Eclipse Console window. To open and view the output of LogCat, open Eclipse menu of Window->Show View->Others->Android->LogCat.
However, although the debug output is available in LogCat, there are quite a lot of other debug output, which really makes it difficult to find the log you are interested. To handle the issue, Log class should be used to write the debug output, since Log class provides a way to specify a log tag for each log entry, and you can filter the entries in LogCat by log tag.

public class myActivity extends Activity {
public static final String MyActivityTag = "MyActivityTag";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.v(MyActivityTag, "MyActivity onCreate");

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

Note you can add multiple log filter by clicking the '+' icon in Log Cat view. For each log filter, a separate log output tab page will be created for showing the log entries belonging to this log tag.

Jonathan

Saturday, February 19, 2011

Reference Android Resource

There are two way to refer a Android resource - from code and from xml resource file.

To refer resource in code, the format is R.Calss.ResourceName. The following are some usage sample.
setcontentview(R.layout.main)

Resources myResources = getResources();
CharSequence styledText = myResources.getText(R.string.stop_message);

To refer resource as attribute in xml, the format is
"@[packagename:]Class/ResourceName" as showing in the following example:



android:text="@string/stop_message" android:textColor="@color/opaque_blue" />


Usually, you do not need to include the package name, unless you need to refer a system defined resource. The following sample referrs to system color darker_gray
android:textColor="@android:color/darker_gray" />

In rare occasion, you may also see a ? in the format as following sample
android:textColor="?android:textColor"

? means using the current textColor defined for the view, so the actual color can only be decided at runtime.

Jonathan

Android Resource ID Format

When defining resource for Android, the format of resource id attribute looks like android:id="@+id/item01"

The '@' at the beginning of the string is just a delimiter, and indicates that the following string is an ID resource.

The '+' means that this is a new resource id that must be created and added to the resource R file. However if the same resource ID are defined more than once, only one ID is included in R file.

The 'id/item01' is the class and data member name included in the generated R class. The resource can be referred as R.id.item01. The class name is required, so just define 'item01' is invalid. If the resource id is defined as android:id="@+idd/ddd/item01", then the generated R file only includes R.idd.ddd as the reource id.

Do not mix resource id with resource name. Resource name is used to identify a resource. Some resource includes a list of items, like menu items in a menu resource, so resource id can be used to identify a particular item in the list.

Jonathan