11 messages in com.googlegroups.android-developers[android-developers] Re: how can i sa...
FromSent OnAttachments
solawe23 Jul 2008 06:27 
hackbod23 Jul 2008 09:57 
solawe24 Jul 2008 03:33 
hackbod24 Jul 2008 15:48 
Steve Oldmeadow24 Jul 2008 18:23 
6real25 Jul 2008 00:46 
solawe25 Jul 2008 03:10 
6real25 Jul 2008 03:30 
solawe25 Jul 2008 05:58 
Steve Oldmeadow25 Jul 2008 19:23 
6real28 Jul 2008 04:35 
Subject:[android-developers] Re: how can i save a class instance in the android application context
From:solawe (ikra@gmail.com)
Date:07/25/2008 05:58:59 AM
List:com.googlegroups.android-developers

Hello 6real; thank you for the response;

I find that it is a very good idea; but the problem for me is that i need to reuse my instance a lot many time in my application,not just at the start/stop;

so i would know if this mecanism affect the performance; ( many save and retrieve of my object in files)

thanks in advance

On 25 juil, 10:31, 6real <cyri@gmail.com> wrote:

Solawe,

so if i repeat your need : -> 1 complex object has to be stored and retrieved when your app stop/ start -> This object must be accessible in your program

If I were you I'll do the follwing -> To store and retrieve your file, use java serialization mechanism. You'll find a lot of information and tutorial on internet. -> This class should be the singleton fo a singleton pattern

You should have a méthode getInstance like this: public class A implements Serializable { A a = null; public static A getInstance(){ if (a = null){ 1/ first try to deserialize the persistence file ... I let you work here :-)

2/ if the file does not exisits, for the first time for instance: a = new A() }//end if return a; }//end method

public save(){ // save your class thanks to serialization in a file }

}//end class

You'll find infiormation on how to store file in locale application context here :http://code.google.com/android/devel/data.html

Every time you need to use your instance call A.getinstance()

Save with A.getinstance().save()

Rgds

C.

On Jul 25, 12:11 pm, solawe <ikra@gmail.com> wrote:

Thank you hackbod, Steve Oldmeadow and 6real for your response;

I've a question : can i acess to an object A stored in a Application object from a simple class java?

because me i need to store an instance of a class A on starting of my android application,later this instance A must be accessed by other simple classes java included on my application.

Thanks for your responses.

On 25 juil, 07:47, 6real <cyri@gmail.com> wrote:

I don't know exactly what will be your final choice but you maybe should think to implements a Singleton pattern :

Personnaly I have a class named ConfigurationContext with only static variables that are stored, for persistence with the preferences mechanism (http://code.google.com/android/devel/data/preferences.html) and : in my first activity the class is loaded. Then all is accessed trough static attributs If one attribut is changed, then I saved the preferences.

I hope it could help you but I only need to store simple object like strings/numbers or boolean.

C.

On Jul 25, 3:23 am, Steve Oldmeadow <st.@smoco.com> wrote:

On Jul 24, 6:33 pm, solawe <ikra@gmail.com> wrote:

Ok, thanks;

But i dont know when i declare this variable? you think that i must declare it on the first activity of my application?

You could create an Application object and store it there. Have a look at android.app.Application. It has an onCreate() that gets called when the Application is created, before any Activities. If it is going to be accessed by Services you probably need to think about threading issues.