Archive for August, 2011

Using newer version of HttpClient on Android (like 4.1.x)

2011-08-20   

[EDIT] 2015-10-15: Now apache provides jar for android (currently 4.3.5.1). Also if you are targeting Android API 23 and newer you can use packages for Android maintained by Marek Sebera (currently 4.4.1.1).

[EDIT] This publication and method are old and deprecated. Please use http://code.google.com/p/httpclientandroidlib/ instead.

If you ended up here that is probably because you hit a bug  in the built-in HttpClient library which is not just old, it is beta quality (httpclient-4.0-beta1.jar). Strangely enough there is no hope that Android team will update it soon (also this is impossible without breaking backward compatibility…).

Fear not, there is a remedy.

Sadly we cannot replace the build-in library nor just add newer version but we can "trick" the system and move the library in another namespace in order to avoid conflict with the built-in lib.

The quick solution: here is an zip file containing all the required files (JARs converted using "ogrelab-" namespace prefix). Jump directly to step 7 in the "thorough" solution bellow.

The thorough solution:

Steps:

1. Download JarJar -- this is the tool which we will use to move the classes in new namespace

2. Download latest HttpClientCommonsLogging and Log4j (you will need "binary" packages) and extract them somewhere

3. Create a temporary dir somewhere like tmp_httpclient for example find following Jars and copy them into the temp dir (versions may differ):

  • commons-logging-1.1.1.jar
  • httpclient-4.1.2.jar
  • httpcore-4.1.2.jar
  • httpmime-4.1.2.jar
  • log4j-1.2.16.jar
Please note: there are additional JARs especially in the HttpClient archive but I don't use them, so I've excluded them from the list. Depending on your situation you may need them at some point so you will have to "convert" them too.
4. Copy jarjar-1.1.jar to the temp dir too (more…)

Activity lifecycle explained in details

2011-08-09   

Activity lifecycleUsually one of the biggest obstacles for the newcommers to android programming is the correct understanding of the activity's lifecycle, for example what is the difference in the event's chain when hitting "Back" button compared to activity being closed when phone is rotated. You cannot obtain this information by looking just at the activity lifecycle diagram in the API ref. In the following post I will use sample project that uses logging to demonstrate exactly what happens in the different cases. It is ment to be used as complementary tool for understanding how activities work.

Before proceeding you may want to download the sample project: Activity Lifecycle Demo. It contains simple logging of the activity and method name in each of the methods related to the lifecycle.

1. "Cold" start

Cold start is the case when the activity was a) started for the first time OR b) started after stopped with hitting "Back" button (the hardware one not <Button> that may exists in some activity's layout).

Logcat will show something like (timestamps will differ):

08-09 17:37:20.500: INFO/TEST(10107): A onCreate
08-09 17:37:20.507: INFO/TEST(10107): A onStart
08-09 17:37:20.507: INFO/TEST(10107): A onResume

Important to note here is that when onCreate(Bundle savedInstanceState) is called savedInstanceState is null. That is because there is no saved state because this is "cold" start (obviously).

2. Hitting "Back" button

08-09 17:50:19.050: INFO/TEST(10107): A onPause
08-09 17:50:19.433: INFO/TEST(10107): A onStop
08-09 17:50:19.433: INFO/TEST(10107): A onDestroy | isFinishing: true

Important to note here is isFinishing: true which means that call to isFinishing() in the onDestroy() returns true, i.e. which happens when: (more…)