Android: possible issues & solutions with configuration changes, part 2
Senin, 05 Maret 2012
0
komentar
Hello! Last time I was quite busy by creation of very cool and dynamic Android application for one of the world's largest mobile telecommunications company.
That is why the second part of the article about possible issues & solutions with configuration changes is coming only now. To be familiar with the topic I recommend you to read the first part of this article.
And now I want to share with you my solution for the mentioned topic. It's easy to use and it solves the task! This solution was evoked by Adobe Flex event model. As you may know I have extensive experience with Adobe Flex (it's Adobe's framework for creating Rich Internet Application). And one of the coolest Adobe Flex feature is its powerful event model. In general all Adobe Flex is event-driven, which gives us the perfect environment for creating loosely coupled components and system.
The same approach I wanted to apply in Android development.
The core idea of my solution is to have some kind of event bus:
Ah no, not like this! I am joking! :D
The event bus we will have can be used to post results and subscribe to results of some specified event type. Thereby we will have loosely coupled system: components can notify and be notified by other components even without knowledge about each other.
So, let's take a look at this approach by example.
Imagine the common situation: you should make some work in the background thread. When a background task is completed you want to notify the caller about it.
How do we handle it with the suggested solution? Fairly simple!
1) define some event name, e.g. STOCKS_UPDATED
2) add a listener (which is a realization of EventCallback interface) to that event:
Few notes:
a) you can add as many event listeners as you want
b) if you don't need listener anymore, just remove it:
If needed you can extend it to carry any kind of data you like. Or just subclass BaseEventResult to more specifiŅ event results (e.g. StockResult, BondResult, etc.)
d) if you keep event listeners as an Activity fields, then, as you already know, they will be destroyed with Activity itself when configuration changes occurs. To handle it: just add a listener at onResume and remove it at onPause.
I've prepared a sample project that demonstrates usage of my EventBus solution. You can download it here.
While this approach may be not perfect, I think it is quite good, because of it covers most of the common issues with configuration changes. Also it's very easy to use and highly extensible! Feel free to use it or share your thoughts about it with me! ;-)
That is why the second part of the article about possible issues & solutions with configuration changes is coming only now. To be familiar with the topic I recommend you to read the first part of this article.
And now I want to share with you my solution for the mentioned topic. It's easy to use and it solves the task! This solution was evoked by Adobe Flex event model. As you may know I have extensive experience with Adobe Flex (it's Adobe's framework for creating Rich Internet Application). And one of the coolest Adobe Flex feature is its powerful event model. In general all Adobe Flex is event-driven, which gives us the perfect environment for creating loosely coupled components and system.
The same approach I wanted to apply in Android development.
The core idea of my solution is to have some kind of event bus:
Ah no, not like this! I am joking! :D
The event bus we will have can be used to post results and subscribe to results of some specified event type. Thereby we will have loosely coupled system: components can notify and be notified by other components even without knowledge about each other.
So, let's take a look at this approach by example.
Imagine the common situation: you should make some work in the background thread. When a background task is completed you want to notify the caller about it.
How do we handle it with the suggested solution? Fairly simple!
1) define some event name, e.g. STOCKS_UPDATED
2) add a listener (which is a realization of EventCallback interface) to that event:
EventBus.subscribe(STOCKS_UPDATED, stocksCallback);
3)just post the result from the background task, when you want to notify listeners about some events (e.g. in onPostExecute method of some AsyncTask): EventBus.postResult(STOCKS_UPDATED, result);That's all! As you can see you can wire up your application components with minimum lines of code in an easy and reliable way!
Few notes:
a) you can add as many event listeners as you want
b) if you don't need listener anymore, just remove it:
EventBus.unsubscribe(STOCKS_UPDATED, mStocksCallback);c) BaseEventResult is a base class for events results. It already has status and data fields.
If needed you can extend it to carry any kind of data you like. Or just subclass BaseEventResult to more specifiŅ event results (e.g. StockResult, BondResult, etc.)
d) if you keep event listeners as an Activity fields, then, as you already know, they will be destroyed with Activity itself when configuration changes occurs. To handle it: just add a listener at onResume and remove it at onPause.
@Override
protected void onResume() {
super.onResume();
EventBus.subscribe(STOCKS_UPDATED, mStocksCallback);
}
@Override
protected void onPause() {
EventBus.unsubscribe(STOCKS_UPDATED, mStocksCallback);
super.onPause();
}
I've prepared a sample project that demonstrates usage of my EventBus solution. You can download it here.
While this approach may be not perfect, I think it is quite good, because of it covers most of the common issues with configuration changes. Also it's very easy to use and highly extensible! Feel free to use it or share your thoughts about it with me! ;-)
TERIMA KASIH ATAS KUNJUNGAN SAUDARA
Judul: Android: possible issues & solutions with configuration changes, part 2
Ditulis oleh Unknown
Rating Blog 5 dari 5
Semoga artikel ini bermanfaat bagi saudara. Jika ingin mengutip, baik itu sebagian atau keseluruhan dari isi artikel ini harap menyertakan link dofollow ke https://androidillegal.blogspot.com/2012/03/android-possible-issues-solutions-with.html. Terima kasih sudah singgah membaca artikel ini.Ditulis oleh Unknown
Rating Blog 5 dari 5
0 komentar:
Posting Komentar