-2

I'm working on a practise 7 days exercise app. I set a progree bar on the main activity and want to change progress after days. Means if i complete exercise of day 1 ,progress bar set 15% . and when i complete day2 , progress set 30%. I can do it without shared preference , it working correctlt but when after day1 complete i closed the app it again set progress from 0 . So i want to use shared preferences for this reason . Kindly someone guide me regarding this issue;

Mughira Dar
  • 103
  • 1
  • 1
  • 6
  • 1
    Please provide the code you tried to store the data in sharedpreference – Ashish Oct 16 '19 at 12:58
  • 2
    `someone guide me` this is wrong place to ask for guide. Be more specific, what exactly is a problem for you? – Vladyslav Matviienko Oct 16 '19 at 12:59
  • 2
    Search by yourself about your problem and get your hands dirty first. If you still are unable to do it, then try the option of asking question on StackOverflow. Have a look at this thread though: https://stackoverflow.com/questions/23024831/android-shared-preferences-example – Ananth Oct 16 '19 at 13:05
  • Thanks anath for giving meadvise – Mughira Dar Oct 16 '19 at 13:12

2 Answers2

0

For Set Value to Shared Prefrence

SharedPreferences.Editor editor = getSharedPreferences("ProgressBarData", 
MODE_PRIVATE).edit();
editor.putInt("progress", 15);
editor.apply();

for get value From Shared Prefrence

SharedPreferences prefs = getSharedPreferences(ProgressBarData, 
MODE_PRIVATE); 
int progress = prefs.getInt("progress", 0);
Hardik Bhalala
  • 209
  • 2
  • 6
0

1st ur know ur mistake u can't store ur data in local variable because at the end of the activity it destroyed every things and when u come back to android activity it will be started all thing once again and every thing is restarted

[https://developer.android.com/guide/components/activities/activity-lifecycle read this u have better understanding

Now ur solution

if u want to store the data and process every day better to use local storage like Sqlite ,room or shared preference.

Step to do the task

There is three step to store, get and remove data into share preference

For storing , getting deleting data


//storing
SharedPreferences.Editor editor = context.getSharedPreferences(name,Context.MODE_PRIVATE).edit();
        editor.putString(key, data);
        editor.apply();

//getting
SharedPreferences getSharedPrefrence = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        int data = getSharedPrefrence.getInt(key, IntegerValuesAndStringValues.REGISTER_BEFORE_LOGIN);
        return data;

BasicFunctions.removeSharedPrefrences(getContext(),"Name of the preference");