c# - Should I use a listener interface or handler for event callbacks in Android development? -


i'm new java, i'm porting on our windows phone 7 library run on android. due syntax similarities has been simple far. our library abstracted http message queue provides data persistence , integrity on mobile platforms. provides asynchronous methods design choice. on wp7 make use of delegates call user supplied callback when async message has been processed , servers response received.

to achieve same thing on android i've found 2 ways far - simple java listener interface contains onsuccess , onfailure methods user must implement, or using android handler class provides message queue between threads (http://developer.android.com/reference/android/os/handler.html).

i've gone handler @ stage if i'm honest similar c# delegate. seems less work user of our library implement. example of user code make use of our library:

connection.getmessage("http://somerestservice.com", getcallback);  handler getcallback = new handler() {     public void handlemessage(message message){         custommessageclass custommessage = (custommessageclass)message.obj;          if(custommessage.status == status.delivered) {            // process message here,             // contains various information transaction             // tag can contain user object etc.             // contains servers response string , byte array.         }     } }; 

using user can create many different handlers they'd like, called whatever they'd like, , pass them in method parameters. similar delegate...

the reason i'm wondering if should move listener interface because more exposure gain java more seems that's how it's done , it's how third parties using our library expect done.

it's same process, except each time wanted different server response i.e. might fetching different types of data different endpoints, you're going have create custom class implements our interface each time, implementing methods our interface has. or of course have single monolithic class server responses funneled in have fun trying figure out each individual response...

i may bit biased due coming c# listener seems bit convoluted , handler implementation better, java developers have thoughts/advice? appreciated.

cheers!

the benefit of using interface approach loose coupling. way, class implements interface shouldn't aware of (or affected by) thread management being done elsewhere , can handle result object appropriate within scope.

btw, i'm big fan of asynctask. have tried using?


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -