切换活动时保持视图活跃

时间:2011-09-28 09:37:55

标签: android view android-activity ads

我创建了一个游戏,我在其中旋转了多个活动。每项活动只停留几秒钟。现在我应该在游戏中添加广告。因为如果广告在几秒钟之后刷新就没有意义,我必须创建一个视图,即使我开始一个新的活动,它仍然保持活着。

由于视图绑定到活动(?),因此可能无法实现。所以我想知道还有另一种解决方案可以在内容视图旋转时保持adView的活跃。

提前致谢。

编辑: 这是一个简单的Activity,它是活动周期的一部分:     公共类惩罚扩展ActivityWithSound实现OnClickListener {

    @SuppressWarnings("unused")
    private final String TAG = "Punish";

    private RelativeLayout buttonContainer;
    private ImageView bgImg;
    private TextView nameTxt;
    private TextView questTxt;
    private Button mainMenuBtn;
    private Button okBtn;

    private Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        bundle = getIntent().getExtras();
        if(bundle == null)
            bundle = StbApp.getTempBundle();
        setContentView(R.layout.quest);
        setupView();
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        soundtrack.startFX(R.raw.fx_execution);
        super.onResume();
    }

    @Override
    protected void onPause() {
        StbApp.getTempBundle().putInt(Victim.VICTIM, bundle.getInt(Victim.VICTIM));
        soundtrack.stopAllFX();
        super.onPause();
    }

    @Override
    public void onClick(View view) {
        if (view == mainMenuBtn){
            //TODO Continue Function
            Intent intent = new Intent(this, TitleScreen.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            StbApp.setContinueBtn(true);
            StbApp.getLastActivity().setClass(this, Punish.class);

            startActivity(intent);
        }

        if (view == okBtn){
            if (StbApp.getPenalty() == PenaltyType.LEAVE){
                StbApp.getPlayer().remove(bundle.getInt(Victim.VICTIM));
                StbApp.setNumberOfPlayer(StbApp.getNumberOfPlayer()-1);
            }
            if (StbApp.getNumberOfPlayer() == 2 && StbApp.getPenalty() == PenaltyType.LEAVE)
                startActivity(new Intent(this, GameOver.class));
            else {
                startActivity(new Intent(this, Round.class));
            }
        }
    }

    @Override
    public void onBackPressed() {
        return;
    }

    private void setupView() {
        float textSize = (float) getResources().getDimension(R.dimen.standard_text_size)/getResources().getDisplayMetrics().scaledDensity;

        buttonContainer = (RelativeLayout) findViewById(R.id.container_button);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) findViewById(R.id.container_button).getLayoutParams();
        params.setMargins(0, 0, 0, (int) (StbApp.AdHeight*1.3));
        buttonContainer.setLayoutParams(params);

        bgImg = (ImageView) findViewById(R.id.imgv_girl);

        Log.d(TAG, "PlayerSize " + StbApp.getPlayer().size());
        nameTxt = (TextView) findViewById(R.id.text_victim_name);
        Log.d(TAG, "PlayerSize " + StbApp.getPlayer().size());
        Log.d(TAG, "PlayerIndex bundle.getInt(Victim.VICTIM) " + bundle.getInt(Victim.VICTIM));
        nameTxt.setText(StbApp.getPlayer().get(bundle.getInt(Victim.VICTIM)).getName());
        nameTxt.setTextSize(textSize);

        questTxt = (TextView) findViewById(R.id.text_quest);
        switch (StbApp.getPenalty()) {
        case LEAVE:
            questTxt.setText(getResources().getString(R.string.punish_leave));
            break;
        case DRNK:
            questTxt.setText(getResources().getString(R.string.punish_drink));
            break;
        case UNDRESS:
            questTxt.setText(getResources().getString(R.string.punish_undress));
            break;
        }

        questTxt.setTextSize(textSize);

        mainMenuBtn = (Button) findViewById(R.id.button_mainmenu);
        mainMenuBtn.setOnClickListener(this);

        okBtn = (Button) findViewById(R.id.button_ok);
        okBtn.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        if (bgImg.getDrawable() != null){
            bgImg.getDrawable().setCallback(null);
            bgImg.setImageDrawable(null);
        }
        super.onDestroy();
    }

我需要的是onDestroyonPauseonResume的替代方案。

1 个答案:

答案 0 :(得分:0)

解决方案可能是使用ViewFlipper / ViewSwitcher而不是跳跃活动,然后将adsView置于ViewFlipper / ViewSwitcher之上或之下 - 但是可能需要对您的应用进行大量重写。