0

After watching a rewarded ad video, a text view displays the rewarded points. The points accumulate after each video. However, when the app closes, the points get erased. I would like the points to continue to show when the user comes back to the application. I could not figure out if this is part of the onDestroy method or savedinstance.

    public class ProfileActivity extends AppCompatActivity implements RewardedVideoAdListener {

    TextView mText, userEmail;
    private int pointCount;

    private TextView dateTimeDisplay;
    private Calendar calendar;
    private SimpleDateFormat dateFormat;
    private String date;

    Button userLogout;
    Button goToHome;
    ImageView ivQR;
    private AdView mAdView;
    FirebaseAuth firebaseAuth;
    FirebaseUser firebaseUser;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;
    private RewardedVideoAd mRewardedVideoAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

     //rewards ads
        //ca-app-pub-9125010107042455/6647636731 actual
        //ca-app-pub-3940256099942544~3347511713 for testing
        MobileAds.initialize(getApplicationContext(), ("ca-app-pub-9125010107042455/6647636731"));
        // Use an activity context to get the rewarded video instance.
        mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
        mRewardedVideoAd.setRewardedVideoAdListener(this);

        //points count
        mText = findViewById(R.id.textView);
        pointCount = 0;
        mText.setText("Points: " + pointCount);

        loadRewardedVideoAd();

     private void loadRewardedVideoAd (){

        if (!mRewardedVideoAd.isLoaded()){
            //ca-app-pub-9125010107042455/6647636731 actual
            //ca-app-pub-3940256099942544/5224354917 for testing
            mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",new AdRequest.Builder().build());
        }
    }

    public void startVideoAd(View view){
        if(mRewardedVideoAd.isLoaded()){
            mRewardedVideoAd.show();
        }
    }

    @Override
    public void onRewardedVideoAdLoaded() {

    }

    @Override
    public void onRewardedVideoAdOpened() {

    }

    @Override
    public void onRewardedVideoStarted() {

    }

    @Override
    public void onRewardedVideoAdClosed() {

        loadRewardedVideoAd();

    }

    private void addPoints(int points) {
        pointCount +=  points;
        mText.setText("Points: " + pointCount);
    }

    @Override
    public void onRewarded(RewardItem rewardItem) {

        Toast.makeText(ProfileActivity.this, " Points ", Toast.LENGTH_SHORT).show();
        addPoints(rewardItem.getAmount());

    }

    @Override
    public void onRewardedVideoAdLeftApplication() {

    }

    @Override
    public void onRewardedVideoAdFailedToLoad(int i) {

    }

    @Override
    protected void onPause() {
        mRewardedVideoAd.pause(this);
        super.onPause();
    }

    @Override
    protected void onResume() {
        mRewardedVideoAd.resume(this);
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        mRewardedVideoAd.destroy(this);
        super.onDestroy();
    }

    @Override
    public void onRewardedVideoCompleted() {

    }
}
Carlos Craig
  • 149
  • 1
  • 14

3 Answers3

2

You should use the onSaveInstanceState(Bundle savedInstanceState) method in order to save the number of points.

You can do so by doing the following:

//This is what will be used to recognize your number of points in the saved bundle.
static final String POINTS = "pointCount";

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save your number of points
    savedInstanceState.putInt(POINTS, pointCount);

    super.onSaveInstanceState(savedInstanceState);
}

And then call the onRestoreInstanceState(Bundle savedInstanceState) method so that you can restore the number of points.

public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    // Restore your number of points and store them in your variable
    pointCount = savedInstanceState.getInt(POINTS);
}

If you want further explanation, refer to the official documentation https://developer.android.com/guide/components/activities/activity-lifecycle#java

1

i would recommend to use SharedPrefrence for this.

you can use this class for your future use.

create a class name: SharedPref & save this code in that class.

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public class SharedPref
{
    private static SharedPreferences mSharedPref;
    public static final String POINTS = "POINTS";

    private SharedPref()
    {

    }

    public static void init(Context context)
    {
        if(mSharedPref == null)
            mSharedPref = context.getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
    }

    public static String read(String key, String defValue) {
        return mSharedPref.getString(key, defValue);
    }

    public static void write(String key, String value) {
        SharedPreferences.Editor prefsEditor = mSharedPref.edit();
        prefsEditor.putString(key, value);
        prefsEditor.commit();
    }

    public static boolean read(String key, boolean defValue) {
        return mSharedPref.getBoolean(key, defValue);
    }

    public static void write(String key, boolean value) {
        SharedPreferences.Editor prefsEditor = mSharedPref.edit();
        prefsEditor.putBoolean(key, value);
        prefsEditor.commit();
    }

    public static Integer read(String key, int defValue) {
        return mSharedPref.getInt(key, defValue);
    }

    public static void write(String key, Integer value) {
        SharedPreferences.Editor prefsEditor = mSharedPref.edit();
        prefsEditor.putInt(key, value).commit();
    }
}

Simply call SharedPref.init() on MainActivity once

SharedPref.init(getApplicationContext());

To Write data

SharedPref.write(SharedPref.POINTS, 25); //save 25 POINTS in shared preference.

To Read Data

int POINTS = SharedPref.read(SharedPref.POINTS, 0); //read POINTS from shared preference, if no value found then it will return 0 as default POINTS.

thanks to: How to use SharedPreferences in Android to store, fetch and edit values

according to your codes the solution will be like:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    SharedPref.init(getApplicationContext());
    // your other codes here.....

modify your addPoints function as below:

private void addPoints(int points) {
    pointCount +=  points;
    SharedPref.write(SharedPref.POINTS, pointCount);
    int POINTS = SharedPref.read(SharedPref.POINTS, 0); // you can make this globle once then use it anywhere in the class..
    mText.setText("Points: " + POINTS);
}

now use this code to show the total points:

int POINTS = SharedPref.read(SharedPref.POINTS, 0);
mText.setText("Points: " + POINTS);
Shiva
  • 88
  • 6
1

You can save your accumulated points into a Database or simply use SharedPreferences to save and load points

to save points

private void savePoints(int totalPoints){
    SharedPreferences prefs = mContext.getSharedPreferences(
            "user_points", Context.MODE_PRIVATE);
    // save points
    prefs.edit().putInt("points", totalPoints).apply();
}

to load saved points

private int getPoints(){
    SharedPreferences prefs = mContext.getSharedPreferences(
            "user_points", Context.MODE_PRIVATE);
    // retrieve points
    // 0 is the default value if nothing was stored before
    return prefs.getInt("points", 0);
}

save points in your addPoints method

private void addPoints(int points) {
pointCount +=  points;

// save points to SharedPrefs
savePoints(pointCount);

mText.setText("Points: " + pointCount);
}

and lastly in your onCreate() change this

pointCount = 0;

to this

pointCount = getPoints();
essayoub
  • 713
  • 2
  • 8
  • 14
  • This is saving the points after close, but after a user is logged out and back in , the points erase, I think I would like to try the option of the database storage. I have firebase and I also use Excel as a database. would it be possible to store in one of those options? – Carlos Craig Feb 11 '20 at 01:56
  • I still marked this a answer because it did what I need, you opened my eyes to a new possibility with the database Idea, that way I could possibly manually award points. I would greatly appreciate your help with that part – Carlos Craig Feb 11 '20 at 01:56
  • 1
    if you're implementing the "log in" and "log out" option it is recommended to use Firebase since you already have it as you mentioned because if you store your points in a Database or SharedPrefences and a user clears the memory of the app from App Info it will erase Database and also SharedPrefs but if you use Firebase the user can even reinstall your app and logs in again and he will still get his last points. So i highly recommend using Firebase to store those points. – essayoub Feb 11 '20 at 17:27
  • perfect, makes sense, would you happen to have can direct me to how to do that? Or, if needed, I could post a new question – Carlos Craig Feb 11 '20 at 22:47