0

we are currently building an application which includes rewarded video system by AdMob. Application has one MainActivity and there are fragments that are constructed on it. Each fragment has a button, each fragment implements "RewardedVideoAdListener". When the buttons are clicked, rewarded videos are loaded and shown. Now the thing is, each button should have different type of rewards.

The both fragments look like this;

    public class FragmentShop extends Fragment implements RewardedVideoAdListener
    {
    private RewardedVideoAd rewardedVideoAd;
    private Button rewardButton;

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    rewardButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(rewardedVideoAd.isLoaded()){
                    rewardedVideoAd.show();
                    rewardButton.setVisibility(View.INVISIBLE);
                }
                else {
                    Toast.makeText(view.getContext(),"Try again!",Toast.LENGTH_SHORT).show();
                    loadRewardedVideoAd();
                }
            }
        });
    }
private void loadRewardedVideoAd() {
        rewardedVideoAd.loadAd("---", //there is our key
                new AdRequest.Builder().build());
    }

 @Override
    public void onRewarded(RewardItem rewardItem) {
        //Some database actions.
        loadRewardedVideoAd();
    }
}

The problem is, no matter which button we click, only the onRewarded() function runs which is in the fragment no 2. So how we can build a structure where each fragment runs their own onRewarded() function. Should we use onRewardedVideoCompleted() at first?

Cloudz0m
  • 91
  • 1
  • 11

1 Answers1

0

Apply this:

private RewardedVideoAd mRewardedVideoAd;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_main, container, false);
        mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(getContext());
        mRewardedVideoAd.setRewardedVideoAdListener(rewardedVideoAdListener);

        rewardButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // todo
                loadRewardedVideoAd();
            }
        });
        return rootView;
    }

    RewardedVideoAdListener rewardedVideoAdListener = new RewardedVideoAdListener() {
        @Override
        public void onRewardedVideoAdLoaded() {
            Toast.makeText(getActivity(), "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
        }

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

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

        @Override
        public void onRewardedVideoAdClosed() {
            Toast.makeText(getActivity(), "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onRewarded(RewardItem reward) {
            Toast.makeText(getActivity(), getString(R.string.on_rewarded_video) + " " +  reward.getAmount() + " " + reward.getType(), Toast.LENGTH_LONG).show();
            // Reward the user.
        }

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

        @Override
        public void onRewardedVideoAdFailedToLoad(int i) {
            Toast.makeText(getActivity(), "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show();
        }
    };

    private void loadRewardedVideoAd() {
        mRewardedVideoAd.loadAd("ca-app-pub-#################/##########",
                new AdRequest.Builder().build());
    }
Sultan Mahmud
  • 1,245
  • 8
  • 18
  • Firstly, thanks for your answer. I changed my code according to yours. I removed "implements RewardedVideo..", I even named mRewardedVideoAd and rewardedVideoAdListener different in both fragments. But still whenever I push the button, only the "onRewarded function which is in secon fragment" works. – Cloudz0m May 19 '19 at 23:27