Я пытаюсь создать обратный вызов из MainActivity во фрагмент.
В MainActivity у меня 5 вкладок.Каждая вкладка имеет отдельный интерфейс через фрагмент.Фрагменты добавляются через viewpager.Моя цель - сообщить фрагмент, когда объявление было загружено в MainAcitivty.После того, как фрагмент получает сообщение, он может отобразить добавление, вызвав метод MainActivity.На данный момент я не хочу отправлять сообщения обратно в MainActivity.
Код MainActivity:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
private ViewPager viewPager = null;
private RewardedVideoAd mRewardedVideoAd;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.setTitle(R.string.app_title);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(R.string.Tab1));
tabLayout.addTab(tabLayout.newTab().setText(R.string.Tab2));
tabLayout.addTab(tabLayout.newTab().setText(R.string.Tab3));
tabLayout.addTab(tabLayout.newTab().setText(R.string.Tab4));
tabLayout.addTab(tabLayout.newTab().setText( R.string.Tab5));
viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
//handling navigation view item event
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setItemIconTintList(null);
assert navigationView != null;
navigationView.setNavigationItemSelectedListener(this);
MobileAds.initialize(this, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
@Override
public void onRewarded(RewardItem rewardItem) {
Toast.makeText(getApplication(),"Ad Rewarded.", Toast.LENGTH_SHORT).show();
loadRewardedVideoAd();
}
@Override
public void onRewardedVideoAdLoaded() {
Toast.makeText(getApplication(),"Ad loaded.", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdOpened() {
Toast.makeText(getApplication(),"Ad opened.", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoStarted() {
Toast.makeText(getApplication(),"Ad started", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoCompleted() {
Toast.makeText(getApplication(),"Ad completed.", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdClosed() {
Toast.makeText(getApplication(),"Ad closed.", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdLeftApplication() {
Toast.makeText(getApplication(),"Ad left application.", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
Toast.makeText(getApplication(),"Ad failed to load.", Toast.LENGTH_SHORT).show();
}
});
loadRewardedVideoAd();
}
public void loadRewardedVideoAd() {
mRewardedVideoAd.loadAd("xxxxxxxxxxxxxxxxxxxxxxxxxxxx",new AdRequest.Builder().build());
}
public void showRewardedVideoAd() {
mRewardedVideoAd.show();
}
@Override
public void onResume() {
mRewardedVideoAd.resume(getParent());
super.onResume();
}
@Override
public void onPause() {
mRewardedVideoAd.pause(getParent());
super.onPause();
}
@Override
public void onDestroy() {
mRewardedVideoAd.destroy(getParent());
super.onDestroy();
}
public interface onToggledListener{
void onToggled();
}
private onToggledListener toggledListener;
public void setOnToggledlistener(onToggledListener listener){
toggledListener = listener;
}
}//end of MainActivity
Код фрагмента:
public class Tab1 extends Fragment implements MainActivity.onToggledListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab1, container, false);
buttonVideo = (Button) view.findViewById(R.id.videoAd);
fab =(FloatingActionButton) view.findViewById(R.id.fab);
return view;
}
//method implemented here
@Override
public void onToggled(){
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
buttonVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showad();// some method
}
});
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
add();//some method
}
});
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}// end of fragment class
Вот как я пытаюсь достичь своей цели, но я не получаю результат.Обратный звонок не работает