views:

31

answers:

2

I am developing an Android app and I am using a library I wrote. This library has a class that contains some static fields. One of them is a API key. This key is used by other classes in my library to make calls on a remote service.

I initialize the API key on my main Activity once when it is created and the savedInstanceState is null.

My problem lies in other activities as they sometimes use the correct API key when making calls with my library and sometimes they do not. It seems as if the API key has not been set.

In particular there is one activity that i call from my preferences activity that always fails as the API key is not set.

Are not static fields maintained across Activities as they are on normal Java applications? I thought that for a specific jvm instance, all static fields are retained. Is the Android platform using new jvm instances for new Activities?

A: 

Hi Έλληνα! Are you accessing the static field in a direct or indirect way?Are other activities mess with the Api key?If so you should synchronized before accessing it

weakwire
Γειά και σε σένα :) i'm setting and getting the member through methods only. And i am setting it only in once place, once (or whenever my main activity is created without a saved state). On all other places, i simply get it.
Savvas Dalkitsis
+1  A: 

Please read:

http://developer.android.com/guide/topics/fundamentals.html#procthread

Your app is running in a process. The process may need to be killed while it is in the background. Your app must correctly save whatever state is appropriate as it goes in the background (via for ex Activity.onSaveInstanceState()) and/or reconstruct its state when later restarting in a new process.

hackbod
i see... I thought only the activity was killed not the entire process.. So i guess i will need to save and restore my static fields in all activities... :(
Savvas Dalkitsis