切换 Activity 时保持视图处于活动状态

发布于 2024-12-06 22:35:05 字数 4258 浏览 1 评论 0原文

我创建了一个游戏,在其中轮流进行多项活动。每个活动仅停留几秒钟。现在我应该在游戏中添加广告。由于如果广告在几秒钟后刷新就没有意义,因此我必须创建一个即使我开始新活动也始终保持活动状态的视图。

由于视图绑定到活动(?),这可能是不可能的。所以我想知道是否有另一种解决方案可以在内容视图旋转时保持 adView 处于活动状态。

提前致谢。

编辑: 这是一个简单的 Activity,它是活动周期的一部分: 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();
    }

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

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 alive 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二手情话 2024-12-13 22:35:05

解决方案可以是使用 ViewFlipper/ViewSwitcher 而不是跳转活动,然后将 adsView 放置在 ViewFlipper/ViewSwitcher 之上或之下 - 然而,它可能需要对您的应用程序进行相当大的重写。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文