使用Intent Extra阻止Adview在其他活动中重新加载

时间:2018-11-29 10:23:10

标签: android android-intent

我需要将已经创建的AdView发送到另一个活动,而不用Intent Extra重新加载AdView。我有一些代码,但是我不知道如何在另一个活动中实现它,以便从创建AdView的第一个活动中获取数据

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private Boolean adLoaded;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adLoaded = false;
 }

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (menuItem != null) menuItem.setBackgroundColor(Color.parseColor("#efefef"));
                menuItem = view;
                menuItem.setBackgroundColor(Color.parseColor("#a2aed3"));
                switch (position) {
                    case 0:
                        Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
                        intent.putExtra("adLoadedInAdsClass", adLoaded);
                        startActivity(intent);
                        break;
                }
            }
        });
    }

public void onResume() {

        super.onResume();



        if (adLoaded) {

            Log.e("Ad", "Skipping");

        }else {

            loadAdvertisement();
        }

    }

    public void loadAdvertisement() {

        AdView adView = (AdView) findViewById(R.id.adView);

        AdRequest adRequest = new AdRequest.Builder()
                .setRequestAgent("android_studio:ad_template").build();

        adView.loadAd(adRequest);

        adLoaded = true;

    }

现在我需要在另一个活动中获取此数据

@Override
        protected void onCreate(Bundle savedInstanceState) {
            Boolean adLoaded = getIntent().getExtras().getBoolean("adLoadedInAdsClass");
        }

0 个答案:

没有答案