oops. forgot to override handleMessage ( ) in the pseudo code above. Let us assume, that in the handleMessage ( ), we decide which processxyz to call, depending on the message received ! sorry..
On Wed, Sep 9, 2009 at 3:01 PM, Android Development <indodr...@gmail.com>wrote: > Hello, > From the conversation above, i understand that having a static reference to > the activity can be used by background threads (that we may spawn), to send > callbacks to the activity (which can then render stuff on the UI). > > However, It would be helpful, if someone can also provide an example of > using Handlers. > > For my understanding so far, I am posting some pseudo code below. Please > feel free to correct me/enhance the code, so that i may understand this > concept better. > > I can create a static handler reference in my activity class like this: > > private static MyHandler handler = new MyHandler(); > > My handler class may have the following structure (assuming that MyHandler > is an inner class of my Activity class): > > public class MyHandler extends Handler > { > public void processCallConnectedEvent ( Message m) > { > // do stuff here..show a dialog that displays "Call Connected" on > the Activity's UI > //sleep here for sometime, say 2 seconds.. > // dismiss the dialog. > } > > public void processCallDisconectedEvent (Message m) > { > // Do the needful here, pop a dialog that displays "Call > Disconnected" > } > > public void processCallTimedOutEvent (Message m) > { > // Do the needful here, pop a dialog that displays "Call Attempt > timed out" > } > > } > > On Tue, Sep 1, 2009 at 10:27 PM, Streets Of Boston < > flyingdutc...@gmail.com> wrote: > >> >> You're welcome :-) >> >> And you can make your show/dismissDialogSmart 'static'. Then you never >> need a reference to an Activity at all. Just call >> OrientationAwareActivity.showDialogSmart(id) >> >> >> On Aug 31, 5:44 pm, CraigsRace <craig...@gmail.com> wrote: >> > > I deal with these situations by just keeping a public static reference >> > > around to the currently active activity. E.g. >> > >> > Brilliant idea!!! That's the solution! >> > >> > And you could abstract it out into a sub class like this (I wish >> > showDialog/dismissDialog weren't marked as final!): >> > >> > public class OrientationAwareActivity extends Activity { >> > private static OrientationAwareActivity ACTIVE_INSTANCE; >> > >> > @Override >> > protected void onCreate(Bundle savedInstanceState) { >> > super.onCreate(savedInstanceState); >> > >> > ACTIVE_INSTANCE = this; >> > } >> > >> > @Override >> > protected void onDestroy() { >> > super.onDestroy(); >> > >> > ACTIVE_INSTANCE = null; >> > } >> > >> > public void showDialogSmart(int id) { >> > if (ACTIVE_INSTANCE != null) { >> > ACTIVE_INSTANCE.showDialog(id); >> > } >> > } >> > >> > public void dismissDialogSmart(int id) { >> > if (ACTIVE_INSTANCE != null) { >> > ACTIVE_INSTANCE.dismissDialog(id); >> > } >> > } >> > >> > >> > >> > }- Hide quoted text - >> > >> > - Show quoted text - >> >> >> > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---