Android equivalent of NSUserDefaults in iOS

后端 未结 1 1959
北荒
北荒 2020-12-07 21:53

I\'d like to save some simple data. On the iPhone, I can do it with NSUserDefaults in Objective-C.

What is the similar command here in Android?

I\'m just sav

相关标签:
1条回答
  • 2020-12-07 22:25

    This is the most simple solution I've found:

    //--Init
    int myvar = 12;
    
    
    //--SAVE Data
    SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);  
    SharedPreferences.Editor editor = preferences.edit();
    editor.putInt("var1", myvar);
    editor.commit();
    
    
    //--READ data       
    myvar = preferences.getInt("var1", 0);  
    

    Where 'context' is the current context (e.g. in an activity subclass could be this).

    0 讨论(0)
提交回复
热议问题