Adnan
Adnan

Reputation: 8729

Sharing a variable in Android application

I have a json file. So, I was parsed the json file and I want to share jsonarray.length() value all the classes in application. So, what is recommended way to do that??

Upvotes: 2

Views: 96

Answers (4)

Joe Malin
Joe Malin

Reputation: 8641

  1. Figure out a way to eliminate the need to store it, by passing it as an argument.
  2. Store it in SharedPreferences.

Upvotes: 1

Flavio Capaccio
Flavio Capaccio

Reputation: 706

You can also use Application class (http://developer.android.com/reference/android/app/Application.html) and store the info you need in a field; anyway I think the best solutions have already been suggested :)

Upvotes: 2

Juned Ahsan
Juned Ahsan

Reputation: 68715

Three options:

First one is to store it in a private static variable and expose a method to share it with others.

Second one is to expose it as a public static variable

Third one is to store in SharedPreference

Upvotes: 1

stinepike
stinepike

Reputation: 54672

Option 1: you can set it public static

Option 2: you can store it in a SharePreference

Upvotes: 5

Related Questions