Thank your very much! BlablaManager actually handles the image editing session information. One task shall have only one session.
I understand your point. Now I quit using singleton & stead using a static field of the main activity to reference the instance of BlablaManager, and then any other activities within my application share the instance with this static field. Currently it works fine. But I don't know whether it's OK as design in Android. Please give your comments. Thanks! On May 27, 10:29 am, Dianne Hackborn <[email protected]> wrote: > On Tue, May 26, 2009 at 7:11 PM, Oceanedge <[email protected]> wrote: > > Yeah, but I think my singleton class can works well with the OS handle > > Task as a single process. But in Android, Task can be cross linux > > process. So my two activity instances are created by two tasks. > > Because you have set up your app to behave this way. > > > But > > they are all created within one Dalvik VM linux process, and Dalvik VM > > didn't distinguishes the static field(mInstance) which reference the > > singleton class instance for two activity instances belongs to two > > different tasks. Then I encounter the problem. > > Yes, all instances of a particular activity run in a single process. This > for the most part a feature, and there are actually a lot of things that use > singletons to take advantage of this. > > > So I think the problem occurs because Android define a Task can be > > cross process, but Dalvik VM still handles static field based on > > process. So do you think this is a little confused? > > It is working exactly as intended, which is fine. > > > Because if we > > think Activity as a module in a Task, for other OS, normally a Task is > > a single process, and that process can have modules(dynamic link > > library). Static variable can also be defined in dynamic link library, > > but that static variable will be map in the process which load the > > library. That static variable won't be shared to different processes > > (Tasks). > > I would suggest not thinking of Android tasks in any way like what you think > tasks are on other platforms. The -only- thing a task is on Android is a > way to organize related activities. > > > > > My singleton class is implemented like this: > > BlablaManager > > { > > > public static BlablaManager getInstance() > > { > > if (mInstance == null) > > { > > mInstance = new BlablaManager (); > > } > > return mInstance; > > } > > private BlablaManager() > > { > > > } > > > private static BlablaManager mInstance = null; > > } > > That's fine, and you get to share one instance of this with all things > running in a process. > > > Matt suggested to use the Application context to synchronize access to > > the singleton. But I still not very clear about how to implement. > > Should I add a context parameter to getInstance() and maintain a list > > of application context reference, so I can return different instance > > of BlablaManager for different application context? > > Honestly, I think at this point you should stop doing whatever you are doing > which is causing your activity to be used in multiple different tasks, > because this sounds like it isn't the behavior you want. But you haven't > really clearly described how you are currently setting up your activity > flow, nor exactly how you want it to work, so it is hard to provide more > help here. > > And if you really do want your activity to be in separate tasks, then you do > indeed need multiple instances of it, and it sounds like whatever this > BlablaManager thing is should just not be a singleton. > > -- > Dianne Hackborn > Android framework engineer > [email protected] > > Note: please don't send private questions to me, as I don't have time to > provide private support, and so won't reply to such e-mails. All such > questions should be posted on public forums, where I and others can see and > answer them. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

