Savvas Dalkitsis
Savvas Dalkitsis

Reputation: 11592

Static parameters not retained across Activities

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?

Upvotes: 0

Views: 351

Answers (2)

hackbod
hackbod

Reputation: 91351

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.

Upvotes: 1

weakwire
weakwire

Reputation: 9300

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

Upvotes: 0

Related Questions