I made a simple counting app where there is Text-view that contains the counted number, and I want to save the counted number so that when I close the app the counted number should be there.
            Asked
            
        
        
            Active
            
        
            Viewed 86 times
        
    0
            
            
        - 
                    3Possible duplicate of [How to use SharedPreferences in Android to store, fetch and edit values](http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – Shamas S Jan 29 '16 at 16:54
2 Answers
2
            
            
        You should use SharedPreferences. Save the count to preference every time it is updated. Read and load the saved value in TextView when app starts next time.
 
    
    
        Viral Patel
        
- 32,418
- 18
- 82
- 110
1
            The best solution for your problem is to use SharedPreference. create another class called SaveCounterValue and copy the following code to that class
public class SaveCounterValue {
static final String PREF_COUNTER= "counter";
static SharedPreferences getSharedPreferences(Context ctx) {
    return PreferenceManager.getDefaultSharedPreferences(ctx);
}
public static void setCounter(Context ctx, int counter)
{
    SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
    editor.putInt(PREF_COUNTER, counter);
    editor.commit();
}
public static Long getCounter(Context ctx)
{
    return getSharedPreferences(ctx).getInt(PREF_COUNTER, 0);
}
}
Then in your activity after conter++ you copy the below code
SaveCounterValue.setConuter(context, counter);
 
    
    
        rakeshdev
        
- 26
- 1
- 6
