Archive for the ‘Java’ Category

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…)

Identifying the view selected in a ContextMenu (in onContextItemSelected() method)

2011-07-12   

Have you ever needed to popup a ContextMenu by longclicking on some of the UI element (different than item in ListView) for example an ImageView instance? At first glance it seems that this can be acomplished quite easy but in reality there is one big problem: you have only one onContextItemSelected() method in your activity that is called every time a context menu item is selected no matter which View requested the ContextMenu via openContextMenu(). This situation is not a problem if just one View is poping a ContextMenu but sometimes you need to popup ContextMenu from several different views. Let me show an example from a real app:

In this layout I have 4 ImageViews. img1 is called "primary" and all other "secondary". By specification I had to (more…)

Пуснах версия 2.0 на "МОЕТО ТОТО 2" (Android app)

2011-02-22   

Вчера мисля, че успях да ликвидирам и последния бъг в новата версия на Моето Тото 2 и тя беше публикувана в андроид маркета. Новите неща включват:

  • възможност за записване на генерираните комбинации;
  • възможност за въвеждане и записване на собствени комбинации (чрез доста удобен интерфейс IMNSHO :-) )

Връзки за сваляне:

Moeto Toto 2

За версия 3.0 очаквайте функционалност за сваляне на числата от тиражите по интернет + автоматична проверка за печалби.

Custom color buttons for Android (9-patch images + example project)

2011-02-15   

On several occasions I had to create simple buttons that use different background color than the default gray. I've tried to google for already cooked solution but quite surprisingly -- I was unable to find one that suits me so I had to create solution by my self (not really, I just combined other solutions in one easy to use packaged solution).

There are three approaches:

1. First approach (bad) -- just set the background to some color like this

The problems:

a) button does not have rounded edges (and does not fit in the look & feel, i.e. looks alien)

b) does not support changes of the color to indicate focus or pressed state.

2. Second approach (not very good) -- create custom drawable definitions in xml file that use gradients (<gradient>). The problem is that buttons don't look quite the same as default buttons -- edges are not quite with the same roundness, margins and paddings are different. When I first tried to create colored buttons I've tried this approach but after 2-3 hours playing with paddings and roundess I finally gived up.

3. Third approach (the right(eous) one) -- Use different 9-patch images for the different colors.

It works very simple: (more…)