0

The problem is that I am trying to pass the name of the user when the adReward is complete to another activity. But I'm stuck in this activity, even though the video ad is loaded. (and it works well while there are no video ad to show). Here is my code:

My java.class

public class TheDay1 extends AppCompatActivity implements RewardedVideoAdListener {

Vibrator vibrator;

private RewardedVideoAd HDay1;
int currentActivity = 0;
boolean flag;
private EditText vname;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_the_day1);
    vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
    MobileAds.initialize(this,
            "ca-app-pub-3940256099942544/6300978111");

    ///ADD BAN
    AdView mAdView = findViewById(R.id.banner1);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    ///////////////////////////////////
    //VIDEO
    HDay1 = MobileAds.getRewardedVideoAdInstance(this);
    HDay1.setRewardedVideoAdListener(this);
    loadRewardedVideoDAY1();

    vname = findViewById(R.id.name);

    final ImageView nm = findViewById(R.id.ID);
    nm.setVisibility(View.INVISIBLE);
    ColorMatrix matrix = new ColorMatrix();
    matrix.setSaturation(0);
    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
    nm.setColorFilter(filter);


    final Button closeAd = findViewById(R.id.closeDay1);
    closeAd.setVisibility(View.INVISIBLE);

    ///////////////////////////////////////////////////////////////////////////////////////////

    closeAd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            vibrator.vibrate(50);
            loadRewardedVideoDAY1();
            if (HDay1.isLoaded()) {
                HDay1.show();
                setCurrent(1);
            }

                String name = vname.getText().toString();
                getusername(name);
        }
    });

}
////////////////////////////////////////////////////////////////
private void Day1() {
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}



private void getusername(String name) {
        Intent intent = new Intent(this, ActivityTwo.class);
        Resources resources = getResources();
        String key = resources.getString(R.string.key_name);
        intent.putExtra(key, name);
        startActivity(intent);
}

private void loadRewardedVideoDAY1() {
    if (!HDay1.isLoaded()) {
        HDay1 = null;
        HDay1 = MobileAds.getRewardedVideoAdInstance(this);
        HDay1.setRewardedVideoAdListener(this);
        ///////////////TEST ID//////////////////
        HDay1.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
    }
}

@Override
public void onRewardedVideoAdLoaded() {
    Log.d("LOADED!", "onRewardedVideoAdLoaded");
}

@Override
public void onRewardedVideoAdOpened() {
    Log.d("OPENED!", "onRewardedVideoAdLoaded");
}

@Override
public void onRewardedVideoStarted() {
    Log.d("STARTED!", "onRewardedVideoAdLoaded");
}

@Override
public void onRewardedVideoAdClosed() {
    loadRewardedVideoDAY1();
    if (currentActivity == 1) {
        Day1();
    }
}

@Override
public void onRewarded(RewardItem rewardItem) {
    loadRewardedVideoDAY1();
    flag = true;
    if (currentActivity == 1) {
        Day1();
    }
}

@Override
public void onRewardedVideoAdLeftApplication() {
  
}

@Override
public void onRewardedVideoAdFailedToLoad(int i) {

}

@Override
public void onRewardedVideoCompleted() {
  
}

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

@Override
protected void onResume() {
    super.onResume();
   
}

public void setCurrent(int val){
    currentActivity = val;
}

Any idea what the problem could be? Thanks in advance

Emma.bk
  • 189
  • 1
  • 1
  • 11

1 Answers1

0

Try below code. Update ProcessData with your intent logic and update oncreate with other data required in your class

public class TheDay1 extends AppCompatActivity implements RewardedVideoAdListener {

    private RewardedVideoAd mRewardedVideoAd;
    private Boolean bRewardVideo = false;


    @Override
    public void onResume() {
        super.onResume();
        
        if (mRewardedVideoAd != null) {
            mRewardedVideoAd.resume(this);
        }

    }

    @Override
    public void onPause() {
        super.onPause();
        if (mRewardedVideoAd != null) {
            mRewardedVideoAd.pause(this);
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        
        if (mRewardedVideoAd != null) {
            mRewardedVideoAd.destroy(this);
        }
    }


    @Override
    public void onRewardedVideoCompleted() {
        //Toast.makeText(this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRewardedVideoAdLoaded() {
        //Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onRewardedVideoAdOpened() {
        //Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onRewardedVideoStarted() {
        //Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onRewardedVideoAdClosed() {
        //Toast.makeText(this, "Reward Video Closed", Toast.LENGTH_SHORT).show();
        // Load the next rewarded video ad.
        if (bRewardVideo) {
            ProcessData();
        } else {
            Toast.makeText(this, "Please View Reward Video to get Reward Points", Toast.LENGTH_LONG).show();
        }
        loadRewardedVideoAd();

    }

    @Override
    public void onRewarded(RewardItem rewardItem) {
        Toast.makeText(this, "Now you can close Ad to Process", Toast.LENGTH_LONG).show();
        bRewardVideo = true;
    }

    @Override
    public void onRewardedVideoAdLeftApplication() {
        //Toast.makeText(this, "onRewardedVideoAdLeftApplication",Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onRewardedVideoAdFailedToLoad(int i) {
        Toast.makeText(this, "Fail to Load Reward Video. Please try again!", Toast.LENGTH_SHORT).show();

    }

    private void loadRewardedVideoAd() {
        bRewardVideo = false;
        mRewardedVideoAd.loadAd(BuildConfig.REWARDVIDEOID,
            new AdRequest.Builder().build());
    }

    private void ProcessData() {
        Snackbar.make(findViewById(R.id.drawer_layout_law), "Please wait while we process the request... ", Snackbar.LENGTH_INDEFINITE).show();
        //WRITE YOUR INTENT CODE HERE
        Intent intent = new Intent(this, ActivityTwo.class);
        Resources resources = getResources();
        String key = resources.getString(R.string.key_name);
        intent.putExtra(key, name);
        startActivity(intent);
    }

    private void ShowRewardVideoDialog() {

        if (mRewardedVideoAd.isLoaded()) {
            mRewardedVideoAd.show();
        } else {
            loadRewardedVideoAd();
            Toast.makeText(mctx, "Fail to Load Reward Video : Please try again", Toast.LENGTH_LONG).show();
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
        mRewardedVideoAd.setRewardedVideoAdListener(this);
        loadRewardedVideoAd();
    }

}
Amod Gokhale
  • 2,346
  • 4
  • 17
  • 32