Android Activity and Service Life-cycle notes

Hello,

In the past, there have been many times that Activity or Service lifecycle confuse me, and I usually have to go back to the corresponding Android SDK documentation pages, which are very analytic and helpful (of course!). In order to clarify what I need to implement, sometimes I have to read the whole document, until I find what I need, making me read stuff that I have already passed through in the past. So, I decided to keep some notes, which I will present here for future reference, and who knows, these might help you! Some parts are written in my own words, while others have been copied from the Android SDK documentation.

Activity Life-cycle

onCreate: This is where the UI is defined. This method is called only the first time that the activity instance is launched and when it has been dropped by the system

onPause: User is leaving the activity, so any changes made, should be committed

onStart: Class members are still alive, as the class was just stopped, so the memory costy resources that have been released, should be re-initialized at this state.

onDestory: Activity is finishing or being destroyed by the system.

Entrire Lifetime:
– Between the onCreate and onDestroy events
Visible Lifetime
– Between the onStart and onStop events
Foreground Lifetime
– Between the onResume and on Pause events. An application might pass frequently from these stages, so no heavey load should be performed during these transitions.

Status transitions:
1. Activity at the top of the stack is active or running
2. Activity lost focus but is still visible, when is paused. Can be killed by the system in extreme low memory situations.
(it maintains all state and member information)
3. Activity has been completely obscured by another activity, then its stopped. It is no longer visible to the user so its
window is hidden and it will often be killed by the system when memory is needed elsewhere.
(it maintains all state and member information)
4. While an activity being in paused or stopped state, it might be dropped by the system, in order to release memory. When viewed again, it has to be completely restarted from its previous state.

Coordinating activities:
– Activity A’s onPause() method executes.
– Activity B’s onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)
– Then, if Activity A is no longer visible on screen, its onStop() method executes.

Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks. This method  is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, which will be passed to the new object through the onCreate(Bundle) method, if the activity needs to be re-created.

Service Life-cycle

onCreate: service is instantiated and setup

onStartCommand(Intent, int, int): service receives a call via startService.

onDestroy: when an activity calls stopService, or the service itself calls stopSelf, or the system decides to destroy the service.

* Using binding, an Activity can connect to a Service and make direct  calls back to the service. This is another method of using services, which cannot be covered in this post. This method of starting and accessing a Service affects the life cycle of the Service.

Two modes of operation depending on the value returned by the onStartCommand method:
– constant START_STICKY is used for services that are explicitly started and stopped as needed.
– constants START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them.

That’s all for now. I have also attached the life-cycle describing diagrams from Android SDK documentation.

Happy android coding!

Advertisements
Posted in Android | Tagged , , , , , , , , | 4 Comments

Android listSelector to TextView widget

Yesterday I spent a lot of time trying to find a way to apply the ListView item highlight effects to a TextView, without any luck.

I first tried it by creating a custom selector in XML, but couldn’t find a way to retrieve the system default colors of different states in order to apply them in my custom selector drawable that I was afterwards assigning to the TextView, by setting it to its backgroundDrawable. But the solution could be “easily” found in the documentation. I navigated to the ListView documentation and I found an XML attribute of the View named listSelector as well as the corresponding setter and getter methods. In the end of the listSelector attribute description there is a mention of the listSelector global attribute resource symbol listSelector. So, here is the answer. Why not use the global listSelector, which is used by default by every ListView widget?

In my TextView configuration, I set the background attribute to the system resource of the list_selector_background:

android:background="@android:drawable/list_selector_background"

Hope this will help people trying to do the same thing!

Good Luck!

Posted in Android | Tagged , , , , | 2 Comments

Adding Eclipse Icon to Ubuntu 11.10 Unity launcher

Hello,

After having installed Ubuntu 11.10 (which I really enjoy btw, for its simplicity and nice UI), I installed Eclipse IDE to start coding :). I immediately noticed that the icon of Eclipse IDE was not loaded on the Unity Launcher. This was quit annoying as I wanted to select “Keep in Launcher” (option shown after doing a right click on the application icon on the Launcher)  and I didn’t like the nice looking icon with the question mark on it! So, after a bit of googling and  experimenting, I managed to create a Unity Launcher item for a custom application, which in my case was Eclipse IDE, and force Unity to use an icon specified by me in the Launcher item configuration.

Instructions:

1.
sudo vim /usr/share/applications/eclipse.desktop

2.
[Desktop Entry]
Name=Eclipse
GenericName=Java IDE
Comment=IDE for developing applications in Java programming language
Exec=/home/user1/eclipse/eclipse
Terminal=false
Type=Application
Icon=/home/user1/eclipse/eclipse.48×48.png
Categories=Utility;
StartupNotify=true

After the above steps, the Eclipse IDE option appeared in the Unity Dash home menu, showing the icon that I had specified in the configuration file. I afterwards selected “Keep in Launcher” option.

In case you need to follow the above instructions for adding an application to Ubuntu Unity Laucher, don’t hesitate to contact me, if you face any problem!

Good Luck

Posted in Linux stuff | Tagged , , | Leave a comment

Productivity tips

Just read this post, which offers some really helpful guidelines for being productive, even when the amount of work and deadlines rises.

7 Things Highly Productive People Do

You are more than welcome to comment on these tips, or propose others!
My conclusion out of this post: No multitasking + Milestone break-down.

Good luck in applying any of these tips in your daily routine!

Posted in Management | Tagged , | Leave a comment