1

I have a settings menu in my app which controls the units used throughout the app - metric or US units. So when the user selects one of these in the options in the menu, I want my app to use the chosen units throughout in display and calculations.

I plan to do this by storing a boolean in sharedpreferences, then check the value of the boolean every time an activity is opened, and then make the appropriate changes.

Is there a better method to go about doing this?

Thanks

ask
  • 1,934
  • 7
  • 29
  • 38
  • sharedpreferences is the only option i can think of... while you can keep one boolean in the application class(extend from application) and update it everytime you open your main activity. that flag can be used across activities. – Tarun May 15 '12 at 11:35
  • No need to share the code. The person is just saving a boolean – user1378730 May 15 '12 at 11:35

3 Answers3

5

Yes you can extends Applications class and store your data over there using Getter and setter.

So that your data will be retained throughout the Application.

public class SocketManager extends Application {
private static SocketManager singleton;
public int mBluetoothState;


public synchronized static SocketManager getInstance(Context context) {
    if (null == singleton) {
        singleton = new SocketManager();
    }
    return singleton;
}

public synchronized void setState(int state) {
    mBluetoothState = state;
}

public synchronized int getState() {
    return mBluetoothState;
}
}

Access it in Activity like :

  SocketManager socketManager = SocketManager.getInstance(this);
  socketManager.setState(10);
  socketManager.getState();

Add your Application to Maanifest file like this :

 <application
  android:name=".SocketManager"
  android:icon="@drawable/first_aid"
  android:label="@string/app_name" >

   <activity .... />

 </application>

Edit :

You should add your class name that extends Application into Application Tag not on Activity Tag

For further refrence check this link

Venky
  • 10,964
  • 5
  • 46
  • 66
  • Works well if the application has multiple activities. – user1378730 May 15 '12 at 11:54
  • This seems to be a good method if I want to store multiple data values. Does this work if the application process is killed? I need my settings to be stored even after the whole application is closed. – ask May 15 '12 at 12:50
  • I'm getting a ClassCastException - cannot cast to android.app.Activity. Any advice? – ask May 15 '12 at 22:36
  • This is how I do it in my application - App app = ((App)getApplicationContext()); where App is the name of my class. – user1378730 May 15 '12 at 23:06
2

You can have a look at Android Storage options: http://developer.android.com/guide/topics/data/data-storage.html

However, it seems like for your case SharedPreferences is OK

pandre
  • 6,335
  • 7
  • 38
  • 48
1

For just a boolean? If its just a single activity calling SharedPreferencesand assigning it would be fine.

If you have multiple activities in an application you could call it once and load it into a static class and call it that way or subclass the Application class.

But even then it's just a boolean and you should do whatever is most convenient for you.

Community
  • 1
  • 1
user1378730
  • 875
  • 10
  • 18