javaandroidadmobinterstitial

How is it possible that Interstitial is not displayed only when the app is launched for the first time


How is it possible that Interstitial is not displayed only when the app is launched for the first time. When it is started a second time or goes from A.java to B.java and from B.java returns to A.java that only then Interstitial is displayed in A.java. Is that possible and can someone show what that java code would look like. Thank you.


Solution

  • It's very easy.

    Just use SharedPreferences with default value true to achieve that. When user opens the app first time, check if the value is false or not.

    So the first time, you won't have any values saved in the SharedPreferences, so it'll give you the true value by default. Then save false to that sharedpreferences.

    So the next time user opens your app & he will get false there and hence, your ad will be loaded.

    Here is the code:

    SharedPreferences sharedPreferences = getSharedPreferences("ADS_PREFS",MODE_PRIVATE);
    

    SharedPreferences object initialized, now check the value:

    if (sharedPreferences.getBoolean("show_key", true)){
          // stored value is true, it means you can show ads (You have not stored any value 1st time, so it will return 'true' by default, if you pass true as shown)
          // load or show ad here
    }
    

    Then in your ad's close or show method, change the variable to false so the second time it won't load or show the ad.

    SharedPreferences.Editor myEdit = sharedPreferences.edit();
    myEdit.putBoolean("show_key", false);
    myEdit.apply();
    

    // The changes have been made. Now the next time user opens your app, it will give false, so the ad won't be loaded or shown.