1

I want to know if its possible to keep information about an app in a while for example.

I have an app that access this file and get information about the choices that the user have made. For example:

I have a button for many events (event is a model), and i want to know if the user clicked in the button even after the application restarts.

I know that it is possible to keep information about login and password. Is possible to do something like this with other information?

Graham Smith
  • 25,627
  • 10
  • 46
  • 69
WitaloBenicio
  • 3,395
  • 5
  • 25
  • 32

2 Answers2

0

Use Shared Preferences. Like so:

Create these methods for use, or just use the content inside of the methods whenever you want:

public String getPrefValue()
{
  SharedPreferences sp = getSharedPreferences("preferenceName", 0);
  String str = sp.getString("myStore","TheDefaultValueIfNoValueFoundOfThisKey");
  return str;
}

public void writeToPref(String thePreference)
{
  SharedPreferences.Editor pref =getSharedPreferences("preferenceName",0).edit();
  pref.putString("myStore", thePreference);
  pref.commit();
}

You could call them like this:

// when they click the button:
writeToPref("theyClickedTheButton");

if (getPrefValue().equals("theyClickedTheButton"))
{
   // they have clicked the button
}
else if (getPrefValue().equals("TheDefaultValueIfNoValueFoundOfThisKey"))
{
   // this preference has not been created (have not clicked the button)
}
else
{
   // this preference has been created, but they have not clicked the button
}

Explanation of the code:

"preferenceName" is the name of the preference you're referring to and therefore has to be the same every time you access that specific preference. eg: "password", "theirSettings"

"myStore" refers to a specific String stored in that preference and their can be multiple. eg: you have preference "theirSettings", well then "myStore" could be "soundPrefs", "colourPrefs", "language", etc.

Note: you can do this with boolean, integer, etc.

All you have to do is change the String storing and reading to boolean, or whatever type you want.

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
0

You can use SharedPreference to save your data in Android.

To write your information

SharedPreferences preferences = getSharedPreferences("PREF", Context.MODE_PRIVATE);
SharedPreferences.Editor   editor = preferences.edit();
editor.putString("user_Id",userid.getText().toString());
editor.putString("user_Password",password.getText().toString());
editor.commit(); 

To read above information

SharedPreferences prfs = getSharedPreferences("PREF", Context.MODE_PRIVATE);
String username = prfs.getString("user_Id", "");

In iOS NSUserDefaults is used to do the same

//For saving

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:your_username forKey:@"user_Id"];
[defaults synchronize];

//For retrieving

NSString *username = [defaults objectForKey:@"user_Id"];

Hope it helps.

ayon
  • 2,180
  • 2
  • 17
  • 32