I am not entirely happy how objects, especially worker objects, that are dependent on an activity needs to behave and the framework that is available for them to detect the activity availability.
Yes #Android #Fragments are good. yes #AndroidFragments are helpful. May be fragments will solve all of it. Even with fragments I believe there is a case to be made for something called "Activity Dependent Objects" or ADOs in Android.
These are some notes from my thought process here. Mainly as a reminders to myself so that when it is time for me to aggregate them into a framework I would have remembered them.
As I have worked with this idea in the last few months, I think it is mildly helpful. For me it clarified the lifecycle of fragments and activities really well.
The early illustrations below are mainly my brain gibberish and they coalesce more as you scroll down. Also the key conclusions are towards the end. If "Activity Dependent Objects" intrigues you read on.
satya - 10/19/2013 5:51:24 PM
Started with looking at weak references
satya - 10/19/2013 5:52:39 PM
A weak reference may be too large a stick to deal with this
satya - 10/19/2013 5:54:04 PM
Idea of internal objects, external objects, thread interactions
satya - 10/19/2013 5:55:40 PM
Difference between recreated and retained objects
satya - 10/19/2013 5:56:43 PM
Activity states, recreated objects, retained objects
satya - 10/19/2013 5:57:31 PM
Behavior on destroy
satya - 10/19/2013 5:58:19 PM
A class structure
satya - 10/19/2013 5:58:54 PM
An object diagram
satya - 10/19/2013 5:59:39 PM
Another observation
satya - 10/31/2013 1:48:14 PM
Some Key classes
Activity Dependent Object (ADO)
DefaultADO
RecreatedADO
IRetainedADO
RetainedADO
MonitoredActivity
MonitoredActivityWithADOSupport
AsyncTaskADO
satya - 10/31/2013 1:53:50 PM
General thought
Much like fragments it is possible to stitch together a number of objects that are retained with an activity as it goes through its life changes and being aware when it is down and when it is up.
By implementing an "Activity Dependent Object" protocol (by implementing the IRetainedADO interface) these objects can become aware of their reflecting their parent activity and hence behave better.
Of course fragments behave very similar and fill that need as well. But my suspicion is that they do it only to one level deep.
I just did this as an exercise to see what it takes. For me the trigger is the onRetained... callback from the activity. I suppose I can extend it by tying it to a retained fragment
One benefit I foresee even if I were to use retained fragments is to extend the activity awareness to any level deep and have those deep object to quickly behave as if they are part of the activity
satya - 10/31/2013 1:55:57 PM
I will post the project soon
Using this approach I have implemented an asynctask that also behaves like a retained object and correctly address the problem of showing its progress on a fragment dialog and handle all test cases well.
So this works in theory. This is too early to form an opinion how useful this is.
Good or bad, you should take a step and see where it leads...it has been fun at least understanding the activity really well....
satya - 11/16/2013 12:02:18 PM
An activity dependent object (ADO) may need to know the activity onCreate callback
Usually a worker object that wants to encapsulate both work and its UI may need only to know when the activity and fragments are fully populated and their state restored.
However there is a catch that might force us to take a look and respond to the onCreate() of the activity.
Usually you want to rely on the activity and fragment to restore the states of the views. To get this right you may need to initialize the UI properly by the end of the onCreate() of the activity. Otherwise restoring state may not work depending on what happened in onCreate. For example whether a view is GONE or visible when it started. Or the initial expected state of a progress bar!
satya - 11/16/2013 12:11:47 PM
Realization of holding on to activity pointers
satya - 11/16/2013 12:40:03 PM
Both fragments and activities can make use of the idea of ADOs
I haven't flushed out, or refactored, the IActivityDependentObject so that it works well for both Activity and a Fragment but it is quite easily doable...
satya - 11/25/2013 3:01:38 PM
You can see the complete source code and implementation of ADOs here
You can see the complete source code and implementation of ADOs here