Properly save battery Life Throughout App’s lifecycle
On every Google IO, many advocates for the Android platform demonstrate and teach us how to build great apps. The term “Great Apps” consists of many aspects – user experience, performance, saving battery life, not wasting expensive 3G quota and more..in this post i’ll try to demonstrate a simple way to not waste battery life in an Animation oriented activity.
Let’s say that after displaying a nifty splash screen, your main activity consists of some animations which act in some sort of an infinite loop, to change pictures, articles, maybe polling data from a web service (asynctask of some sort). needless to say, that all of these processes take a lot of juice from the battery, so why worry about it?
well, if we are focused in that activity, there’s not much we can do because it’s in the foreground and the user probably has some interactions with the current activity. but what happens once it’s not focused anymore? all these tasks don’t just stop themselves, it’s in our responsibility to manage them via the lifecycle of the current activity.
Basically, the Activity class has some built in callback functions which are fired by the OS itself, you are all probably familiar with onCreate() which sis fired at the beginning of an Activity.
For example, I’ll discuss an Activity which displays some images in a fade in / fade out transition infinitely.
we want to stop these animations once we:
1. start a different Activity
onPause() is called
2. push the home button to return to the application/desktop
onStop() is called
lets assume we have a Handler which posts an animation loop message to the Messages queue of the UI thread:
1:
2: private Runnable dayTitlesRunnable = new Runnable()
3: {
4: @Override
5: public void run(){
6: if (SdpApp.getDays().size()>0){
7: pbDayTitles.clearAnimation();
8: pbDayTitles.setVisibility(View.GONE);
9: tvRotatingdayTitle.setVisibility(View.VISIBLE);
10: if (indexOfDay < SdpApp.getDays().size()-1){
11: indexOfDay++;
12: }else {
13: indexOfDay = 0;
14: }
15: tvRotatingdayTitle.setText("text");
16: }
17: dayTitilesHandler.postDelayed(this, 5000);
18: }
19: };
ok, we have a runnable which we can post to the UI thread, for this we need to define a Handler:
1: private Handler dayTitilesHandler
2: = new Handler();
in out onCreate() method we’ll call:"
1: private void animateDayTitles() {
2: dayTitilesHandler.post(dayTitlesRunnable);
3: }
this function will add the Runnable we’ve defined to the UI Thread’s Message queue.
now, once we lost the focus in the current activity and would like to stop this battery/quota consuming process we need to add some code to the onPause(), onStop(), onResume() callbacks:
1: @Override
2: protected void onPause() {
3: super.onPause();
4: dayTitilesHandler.removeCallbacks
5: (dayTitlesRunnable);
6: }
The removeCallbacks(runnable) is a method of the handler which removes all of it’s callbacks.
1: @Override
2: protected void onResume() {
3: super.onResume();
4: dayTitilesHandler.post(dayTitlesRunnable);
5: }
in the onResume() we post the same runnable to the UI' Thread’s message queue to start the animations again.
so it’s important to remember the the SDK offers us with all the hooks required to build great apps and it’s a great idea to use them in order to manage all the important aspects of application development as mentioned at the beginning of the post.
Comments