Androider Androider - 4 months ago 13
Android Question

Hide / Show SrartApp ad after the user make a donation / didn't donate

I have a StartApp ad in the

MainActivity.java
, and a Donate option in another activity. I want to hide the ad in the
MainActivity
after the user make a donation from the another activity (Named:
About
). I used
SharedPreferences
to specify whether the user donate or not. But I have a problem with the ad, it doesn't appear even if the user didn't pay!

* Hint: There is no problem with the ad before I added the code below.

The code below explains how I used the
SharedPreferences
,
hideBanner();
method, etc....

MainActivity.java:

...
protected SharedPreferences prefs;
protected static final String PREFS_KEY = "donation";
private Banner banner;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StartAppSDK.init(this, "...", false);
setContentView(R.layout.activity_main);

prefs = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);

if(prefs.getBoolean("don_ban", true))
{
banner = (Banner) findViewById(R.id.startAppBanner);
if(banner != null)
{
banner.hideBanner();
}
}
else
{
if(banner != null)
{
banner.showBanner();
}
}

...


About.java:

...
protected SharedPreferences prefs;
protected SharedPreferences.Editor editor;

...

private IabHelper.OnConsumeFinishedListener consumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
@Override
public void onConsumeFinished(Purchase purchase, IabResult result) {

if(result.isSuccess())
{
prefs = getSharedPreferences(MainActivity.PREFS_KEY, Context.MODE_PRIVATE);
editor = prefs.edit();
editor.putBoolean("don_ban", true);
editor.commit();
}
else
{
Snackbar.make(findViewById(R.id.coordinatorlayout_about), R.string.unknown_error, Snackbar.LENGTH_SHORT).show();
}
}
};
...

Answer

Once the user donates, you are setting don_ban to true. If don_ban is true, you hide the banner. The problem is, your default is true when you get don_ban from the SharedPreferences. don_ban has never been created before the user donates, therefore, will return the default value of true and will hide the banner.

Change your code to this and it will work:

...
protected SharedPreferences prefs;
    protected static final String PREFS_KEY = "donation";
    private Banner banner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StartAppSDK.init(this, "...", false);
        setContentView(R.layout.activity_main);

        prefs = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);

        if(prefs.getBoolean("don_ban", false)) //Changed default to false
        {
            banner = (Banner) findViewById(R.id.startAppBanner);
            if(banner != null)
            {
                banner.hideBanner();
            }
        }
        else
        {
            if(banner != null)
            {
                banner.showBanner();
            }
        }

...

All I did was change your if statement's prefs.getBoolean() default to false so it shows the ad by default, instead of hiding it.

Comments