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() {
}
}
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