Reputation: 163
My app consists of two activities: MainActivity and RoomOne, When the user press on a button a Rewarded Video Ad is shown, and as a reward the user is allowed access to the RoomOne Activity, however when the user is taken to RoomOne activity, when he comes back to the MainActivity he can't go to RoomOne again because the Rewarded Video Ad refuses to show, so it goes like this:
1-User launches the app.
2-User wants to access RoomOne so press on a button.
3-Rewarded Video Ad shows successfully, when it finishes the Ad rewards the user by taking him to RoomOne.
4-User has finished what he wanted to do in RoomOne and press the back button to comeback to MainActivity.
5-He is taken to MainActivity.
6-Now he wants to go back to RoomOne, but he can't because when he presses the button the Ad doesn't show, Now he needs to restart the app to be able to get in again.
I tried to figure out what is going on with Toasts and logs, and it seems that when an AdMob Rewarded video is loaded in an activity and then the user goes to another activity and comes back to the original Activity the SDK fails to load the AD. My SDK is set correctly, and it loads Rewarded Videos normally however it seems to break when the user switches activities, but if he stays on the same activity AD videos shows normally one after the other.
My Code:
public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener{
private RewardedVideoAd mAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialise Rewarded Video
MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544/5224354917");
mAd = MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(this);
loadRewardedVideo();
}
private void loadRewardedVideo(){
if(!mAd.isLoaded()){
mAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
}
}
public void ButtonPressed(View v) {
if(mAd.isLoaded()){
mAd.show();
}
}
@Override
public void onRewardedVideoAdLoaded() {
}
@Override
public void onRewardedVideoAdOpened() {
}
@Override
public void onRewardedVideoStarted() {
}
@Override
public void onRewardedVideoAdClosed() {
loadRewardedVideo();
}
@Override
public void onRewarded(RewardItem rewardItem) {
//updateBalance();
//startTimer();
startActivity(new Intent(MainActivity.this, RoomOne.class));
}
@Override
public void onRewardedVideoAdLeftApplication() {
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
}
@Override
protected void onPause() {
mAd.pause(this);
super.onPause();
}
@Override
protected void onResume() {
mAd.resume(this);
super.onResume();
}
Any help would be appreciated, Thanks!
Upvotes: 2
Views: 2110
Reputation: 3444
Load rewarded video ad in onResume instead of onCreate:
@Override
protected void onResume() {
super.onResume();
loadRewardedVideo();
}
Upvotes: 1
Reputation: 327
Just use Flag With Boolean if user is rewarded then set flag==true, then use condition like if (flag==true) then you can allow user to Room Activity Like,
@Override
public void onRewarded(RewardItem rewardItem) {
flag=true;
startActivity(new Intent(MainActivity.this, RoomOne.class));
}
now, when user back from Room Activity You have to manage this Flag like
(i created button for redirect RoomActivity you can use your intent)
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(flag==true){
startActivity(new Intent(MainActivity.this, RoomOne.class));
}else {
Toast.makeText(MainActivity.this, "Plaese watch Video First", Toast.LENGTH_SHORT).show();
}
}
});
Upvotes: 2