mttkay / ignition

Kick-starts Android application development.
1.28k stars 290 forks source link

App design question: Reconnectable dialogs? #9

Closed odysseus654 closed 12 years ago

odysseus654 commented 12 years ago

I'm in the process of adapting my existing code to work with IgnitedAsyncTask (you deprecated android-fu already?) and I'm running into a potential design issue in that most of my asyncTasks are involved in some form of ProgressDialog or AlertDialog, which is tied to the Context object and likely should not survive a reconfiguration.

Is there value in developing a ProgressDialog-like and/or AlertDialog-like object "in the style" of IgnitedAsyncTask that supports .connect() and .disconnect(), recreating the "real" dialog object as-needed (and hooking all the events into itself) ? Or is there a better way to look at this issue that I'm not seeing?

This is not a request for development or a pull request (yet), just a query as to whether this is a good direction to go in, as an issue on a project that seems to deal with these issues as one of its central purposes.

mttkay commented 12 years ago

Hi,

Android has the concept of managed dialogs. You create a managed dialog in Activity.onCreateDialog, and Android will make sure that the dialog will survive configuration changes (i.e. if it was showing during a configuration change, Android will take care of recreating it upon Activity restart). I was actually about to extend IgnitedAsyncTask and ignited-async-task-sample to show how one can leverage IgnitedAsyncTask+IgnitedDialogs+onCreateDialog to have a managed progress dialog show up while a task is running.

odysseus654 commented 12 years ago

I absolutely love the quality of documentation Android has for this, had to read source to figure out what the heck it's trying to do. It does seem like onCreateDialog is used more for assisting in serialization/deserialization of a dialog than a persistent dialog object (although outside an AsyncTask there may not be that much of a difference)

I'm seeing deprecation warnings on nearly every documentation lookup I'm doing here. I know that IgnitedAsyncTask completely unrelated to a Fragment (so using onRetainNonConfigurationInstance still makes sense) but could DialogFragment be useful here?

odysseus654 commented 12 years ago

I threw this together last night, a class "in the style of" AlertDialog.DialogBuilder, intended to be used inside IgnitedAsyncTask (and chained to its connect/disconnect), I'm hoping I'm not going too far "off the rails" here (not yet tested yet): http://pastebin.com/CSd5r5R4

Is there a reason why IgnitedAsyncTask is not using weak references to further reduce the potential for leaked contexts? I haven't used weak references much in java yet, but I would think that they were usable in here.

Lemmie know if this issue is no longer a question and can simply be closed. You are also free to grab the code if you like it (I have a ProgressAlert subclass written/untested as well)

mttkay commented 12 years ago

sorry was gone for a week. The reason why I didn't use weak or soft references is that the premise of the callbacks should be to always have access to a valid context. Well, whether or not it's actually valid depends on whether one called connect/disconnect in the right way, true. In any case though, the reference should not be null, if that was the case, one could use a classic AsyncTask instead where there is no guarantee to have a context reference in e.g. onPostExecute.

mttkay commented 12 years ago

Closed as not being a bug. Feel free to further discuss this on the mailing list.