Промежуточная реклама Admob не отображается при переключении между фрагментом и активностью - PullRequest
0 голосов
/ 08 января 2020

До сих пор я не имел никакого представления о admob в приложении android. Это впервые для меня.

Вот DashboardFragment. java

public class DashboardFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener{

private DashboardViewModel dashboardViewModel;
public static final String API_KEY= "e96f2eb28f2540bf98757628abd274b3";
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private List<Article> articles = new ArrayList<>();
private Adapter adapter;
private String TAG = DashboardFragment.class.getSimpleName();
private SwipeRefreshLayout swipeRefreshLayout;
private RelativeLayout errorLayout;
private ImageView errorImage;
private TextView errorTitle,errorMessage;
private Button btnRetry;
private Switch myswitch;
private InterstitialAd interstitialAd;

@SuppressLint("ResourceAsColor")
public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    dashboardViewModel =
            ViewModelProviders.of(this).get(DashboardViewModel.class);
    View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
    Toast.makeText(getActivity(), "Origin News", Toast.LENGTH_SHORT).show();
    super.onCreate(savedInstanceState);
    swipeRefreshLayout=root.findViewById(R.id.swipe_refresh_layout);
    swipeRefreshLayout.setOnRefreshListener(this);
    swipeRefreshLayout.setColorSchemeColors(R.color.colorAccent);
    recyclerView = root.findViewById(R.id.recyclerView);
    layoutManager = new LinearLayoutManager(getActivity()   );
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setNestedScrollingEnabled(false);

    setHasOptionsMenu(true);
    onLoadingSwipeRefresh("");

    errorLayout = root.findViewById(R.id.errorLayout);
    errorImage = root.findViewById(R.id.errorImage);
    errorTitle = root.findViewById(R.id.errorTitle) ;
    errorMessage = root.findViewById(R.id.errorMessage);
    btnRetry = root.findViewById(R.id.btnRetry);
    MobileAds.initialize(getActivity(),"ca-app-pub-6862851420275633~8406415898");
    interstitialAd = new InterstitialAd(getActivity());
    interstitialAd.setAdUnitId("ca-app-pub-6862851420275633/3227253163");
    interstitialAd.loadAd(new AdRequest.Builder().build());


    return root;
}
public void LoadJson(final String keyword){
    errorLayout.setVisibility(View.GONE);
    swipeRefreshLayout.setRefreshing(true);

    final ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
    String country = Utils.getCountry();
    String language =Utils.getLanguage();
    Call<News> call;

    if(keyword.length()>0){
        call = apiInterface.getNewsSearch(keyword,language,"publishedAt",API_KEY);
    }else{
        call= apiInterface.getNews(country,API_KEY);
    }

    call.enqueue(new Callback<News>() {
        @Override
        public void onResponse(Call<News> call, Response<News> response) {
            if(response.isSuccessful() && response.body().getArticle() != null){
                if(!articles.isEmpty()){
                    articles.clear();
                }
                articles = response.body().getArticle();
                adapter = new Adapter(articles,getActivity());
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();

                initListner();
                swipeRefreshLayout.setRefreshing(false);

            }else{
                swipeRefreshLayout.setRefreshing(false);

                String errorCode;
                switch (response.code()){
                    case 404:
                        errorCode = "404 not found,don't worry you are on Origin";
                    case 500:
                        errorCode = "505 server broken,don't worry you are on Origin";
                    default:
                        errorCode="unknown error,don't worry you are on Origin";
                        break;
                }
                showErrorMessage(R.drawable.no_result,
                        "no result",
                        "Please Try Again\n"+
                                errorCode);

            }
        }

        @Override
        public void onFailure(Call<News> call, Throwable t) {
            swipeRefreshLayout.setRefreshing(false);
            showErrorMessage(R.drawable.no_result,
                    "Oops!",
                    "Network failure,Please Try Later,don't worry we will back soon. \n Origin"+ t.toString());

        }
    });

}
private void initListner(){
    adapter.setOnItemClickListener(new Adapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            ImageView imageView =view.findViewById(R.id.img);
            Intent intent = new Intent(getActivity(), NewsDetailActivity.class);

            Article article = articles.get(position);
            intent.putExtra("url",article.getUrl());
            intent.putExtra("title",article.getTitle());
            intent.putExtra("img",article.getUrlToImage());
            intent.putExtra("date",article.getPublishedAt());
            intent.putExtra("source",article.getSource().getName());
            intent.putExtra("author",article.getAuthor());

            Pair<View, String> pair = Pair.create((View)imageView, ViewCompat.getTransitionName(imageView));
            ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(
                    getActivity(),
                    pair


            );
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){

                startActivity(intent,optionsCompat.toBundle());

            }else{
                startActivity(intent);
            }

        }
    });
}


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)  {
    inflater.inflate(R.menu.menu_main,menu);
    SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
    searchView.setQueryHint("Search Latest News...");
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            if(query.length()>2){
                onLoadingSwipeRefresh(query);
            }else{
                Toast.makeText(getActivity(), "Type More than two letters", Toast.LENGTH_SHORT).show();

            }
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });
    searchMenuItem.getIcon().setVisible(false,false);
    super.onCreateOptionsMenu(menu,inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item){

    switch (item.getItemId()){
        case R.id.action_settings:
            startActivity(new Intent(getActivity(), SettingsActivity.class));
            return true;
        case R.id.dark_mode:
            if(interstitialAd.isLoaded())
            {
                interstitialAd.show();
            }
            else {

                startActivity(new Intent(getActivity(), DarkMode.class));
            }

            return true;

        default:
            return super.onOptionsItemSelected(item);

    }

}


@Override
public void onRefresh() {
    LoadJson("");


}
private void onLoadingSwipeRefresh(final String keyword){
    swipeRefreshLayout.post(
            new Runnable() {
                @Override
                public void run() {
                    LoadJson(keyword);

                }
            }
    );
}
private void showErrorMessage(int imageView,String title,String message){
    if(errorLayout.getVisibility()==View.GONE){
        errorLayout.setVisibility(View.VISIBLE);
    }
    errorImage.setImageResource(imageView);
    errorTitle.setText(title);
    errorMessage.setText(message);
    btnRetry.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            onLoadingSwipeRefresh("");
        }
    });
}

}

Промежуточная реклама Admob не показывается при переключении между Фрагментом и активностью, когда я нажимаю на Darkmode, не загружается класс Darkmode.

Заранее спасибо.

...