1

I am trying to make promo code for my app when the user gives correct value in the EditText I will set a boolean value false and a number will be stored in shared preferences. But don't know why the value is not getting decremented and even if it decreases and even if I do it multiple times then only it moves from 30 to 29. So I created a test app where I am setting the value in onClick what happens when the promo code is equal. So the decrement thing is when the user open the app the number will get decreased and stored back when the boolean value is false

public class MainActivity extends AppCompatActivity {
   TextView textView;
    Button button, button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);
        button = (Button) findViewById(R.id.button);
        button1 = (Button) findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences prefs = getSharedPreferences("Admob", 0);
                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean("ShowAd", false);
                editor.putLong("daycounter", 30);
                editor.commit();
            }
        });
        caller();
    }

    @Override
    protected void onResume() {
        super.onResume();
        caller();
    }

    @Override
    protected void onPause() {
        super.onPause();
        caller();
    }


    public void caller() {
        SharedPreferences settings = getSharedPreferences("Admob", 0);
        boolean ad_start = settings.getBoolean("ShowAd", true);
        long ad = settings.getLong("daycounter", 0);

        SharedPreferences prefs = getSharedPreferences("apprater", 0);

        SharedPreferences.Editor editor = prefs.edit();

        Log.e("toaster", "" + ad_start);
        if (!ad_start) {
            long ads = ad -1 ;

            if (ad > 0) {
                editor.putLong("daycounter", ads);
                editor.putBoolean("ShowAd", true);
                editor.commit();
                Toast.makeText(MainActivity.this, "" + ads, Toast.LENGTH_SHORT).show();
                textView.setText(ad + "R" + ad_start);
            }
        }

    }
}

In the dummy app I am showing the data in TextView so I am calling onResume and onPause method otherwise I know onCreate is enough. I don't understand whats wrong in the algorithm. I am not getting any error at all and in the toast I am only able to decrease the value till 29 . I have tried all types of decrement operation. Any advice will be helpful.**I am able to save the data onle problem is with the lonng value is not getting saved and not getting decremented **

Neelay Srivastava
  • 1,041
  • 3
  • 15
  • 46

3 Answers3

1

Put these methods in your utils class and use

  public static void cacheBoolean(Context ctx, String k, Boolean v) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    prefs.edit().putBoolean(k, v).apply();
}

public static Boolean getCachedBoolean(Context ctx, String k, Boolean defaultValue) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    return prefs.getBoolean(k, defaultValue);
}

public static void cacheString(Context ctx, String k, String v) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    prefs.edit().putString(k, v).apply();
}

public static String getCachedString(Context ctx, String k, String defaultValue) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    return prefs.getString(k, defaultValue);
}

public static void cacheInt(Context ctx, String k, int v) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    prefs.edit().putInt(k, v).apply();
}

public static int getCachedInt(Context ctx, String k, int defaultValue) {
    SharedPreferences prefs = getSharedPreferences(ctx);
    return prefs.getInt(k, defaultValue);
}

public static void clearCachedKey(Context context, String key) {
    getSharedPreferences(context).edit().remove(key).apply();
}
Ahmed Karam
  • 386
  • 1
  • 7
  • sir its simple sharepreference i dont think its of any use .i know how to save the data and i am able to save and get the data but the long value is not getting saved – Neelay Srivastava Mar 12 '19 at 18:47
  • Seems a bit self promotional but you can use some libraries like [this](https://github.com/amin-amini/EasyPrefs), instead of writing this much of boilerplate code :D – Amin Mar 24 '19 at 06:24
0

You don't need to put shared preferences everywhere, just initialize it inside onCreate method and access from constant String, so will easy to access it.

public class MainActivity extends AppCompatActivity {
   TextView textView;
    Button button, button1;
   SharedPreferences prefs;
   SharedPreferences.Editor editor;
   final String pref_name = "Admob";
   final String ad_name = "ShowAd";
   final String day_count = "daycounter";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);
        button = (Button) findViewById(R.id.button);
        button1 = (Button) findViewById(R.id.button2);
        prefs = getSharedPreferences(pref_name, 0);
        editor = prefs.edit();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editor.putBoolean(ad_name, false);
                editor.putLong(day_count, 30);
                editor.commit();
            }
        });
        caller();
    }

    @Override
    protected void onResume() {
        super.onResume();
        caller();
    }

    @Override
    protected void onPause() {
        super.onPause();
        caller();
    }


    public void caller() {
        boolean ad_start = prefs.getBoolean(ad_name, true);
        long ad = prefs.getLong(day_count, 0);

        Log.e("toaster", "" + ad_start);
        if (!ad_start) {
            long ads = ad -1 ;

           if (ad < 0) {
              editor.putBoolean(ad_name, true);
            }
           editor.putLong(day_count, ads);
           editor.commit();
           Toast.makeText(MainActivity.this, "" + ads, Toast.LENGTH_SHORT).show();
           textView.setText(ad + "R" + ad_start);
        }

    }
}
Denny Kurniawan
  • 1,581
  • 2
  • 15
  • 28
-1

I was making mistake in this method part thx to Ahmed sir My mistake was setting the value equal to
if (ad > 0) so when the value is more than zero the boolean was st as true and the data never get a chance to get decremented more

 public void caller() {
    SharedPreferences settings = getSharedPreferences("Admob", 0);
    boolean ad_start = settings.getBoolean("ShowAd", true);
    long ad = settings.getLong("daycounter", 0);


    Log.e("toaster", "" + ad_start);
    if (!ad_start) {
        SharedPreferences prefs = getSharedPreferences("Admob", 0);

        SharedPreferences.Editor editor = prefs.edit();
        long ads = ad - 1;

        if (ad < 0) {
            editor.putBoolean("ShowAd", true);
        }
        editor.putLong("daycounter", ads);
        editor.commit();
        Toast.makeText(MainActivity.this, "" + ads, Toast.LENGTH_SHORT).show();
        textView.setText(ad + "R" + ad_start);
    }

}
Neelay Srivastava
  • 1,041
  • 3
  • 15
  • 46