开发者

Keep a view alive while switching activity

I created a game, where I rotate through multiple activitys. Each activity stays just for a few seconds. Now I should add ads to the game. Since it doesn't make sense if the ad refreshes after just a few seconds I have to create a view which stays al开发者_JAVA技巧ive the whole time even if I start a new activity.

Since a view is bind to an activity (?) it might not be possible. So I wonder wether there is another solution to keep the adView alive while the content views are rotating.

Thanks in advance.

Edit: Here is a simple Activity which is part of the activity cycle: public class Punish extends ActivityWithSound implements 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();
    }

What I need would be an alternative for onDestroy, onPause and onResume.


A solution could be using a ViewFlipper/ViewSwitcher instead of jumping activities and then placing the adsView over or under the ViewFlipper/ViewSwitcher - It will however probably require quite a large re-write of your app.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜