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. 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.
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? 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).
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;
}
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?
Please help, Thanks!
On May 26, 4:04 pm, Romain Guy <[email protected]> wrote:
> The singleton is a perfectly valid pattern withAndroid, but if you
> need to use two instances of your "singleton," then the problem is
> that you are using a singleton in the first place.
>
>
>
> On Tue, May 26, 2009 at 12:58 AM, Oceanedge <[email protected]> wrote:
>
> > Thanks, but I think singleton is a general pattern used generally. Why
> > should we stop using it inAndroid? :(
>
> > On May 26, 12:12 pm, Romain Guy <[email protected]> wrote:
> >> Hi,
>
> >> You cannot do this, if only for security reasons. Running arbitrary
> >> code in the Home process would be very bad :)
>
> >> Instead of creating two processes (which is really heavy and requires
> >> a lot more memory), why don't you stop using a singleton?
>
> >> On Mon, May 25, 2009 at 9:06 PM, Oceanedge <[email protected]> wrote:
>
> >> > I'm developing a photo editor application. It will launched by
> >> >android.intent.action.EDIT intent. I made two application to emit that
> >> > intent. I found that two instance ofmyphoto editoractivityis
> >> > created within the same process which is named asmyactivity. But
> >> > there is a singleton class used inmy photo editoractivityand that
> >> > two activities use the same singleton class instance which breakmy
> >> > application logic.
> >> > I wonder if there is any way toletAndroidcreatemyactivitywithin
> >> > the process which the launcheractivitylies in? So that the two
> >> > instance ofmyphoto editoractivitycan be separated into two
> >> > different process. And so they can reference to different instance of
> >> > that singleton class.
> >> > Thanks!
>
> >> --
> >> Romain Guy
> >>Androidframework engineer
> >> [email protected]
>
> >> Note: please don't send private questions to me, as I don't have time
> >> to provide private support. All such questions should be posted on
> >> public forums, where I and others can see and answer them
>
> --
> Romain GuyAndroidframework engineer
> [email protected]
>
> Note: please don't send private questions to me, as I don't have time
> to provide private support. 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
-~----------~----~----~----~------~----~------~--~---