2011-09-28 98 views
1

我創建了一個遊戲,我在其中旋轉了多個活動。每個活動只停留幾秒鐘。現在我應該在遊戲中添加廣告。由於在幾秒鐘後廣告刷新沒有意義,因此即使我開始新的活動,我也必須創建一個始終保持活動狀態的視圖。在切換活動時保持視圖活躍

由於視圖綁定到活動(?),因此可能無法進行。所以我想知道是否有另一種解決方案可以在內容視圖旋轉時保持adView活着。

在此先感謝。

編輯: 下面是一個簡單的活動也就是活動週期的一部分: 公共類懲治擴展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(); 
    } 

我需要什麼將是onDestroy,和onResume的替代品。

回答

0

解決方案可以使用ViewFlipper/ViewSwitcher,而不是跳過活動,然後將adsView放置在ViewFlipper/ViewSwitcher下方或下方 - 但它可能需要對應用程序進行相當大的重寫。

+0

首先感謝您的答案。 我有一個內存泄漏,並藉助意圖'CLEAR_TOP'和'onDestroy'方法解決了它。此外,我保存活動的狀態,因爲我可以隨時返回到菜單並稍後恢復遊戲。 如果我使用'ViewSwitcher' /'ViewFlipper'有沒有像'ViewSwichterListener','OnViewChange'等?那麼它可能不是那麼多的工作。不幸的是我找不到像這樣的東西。 – Xazen

+0

根據你想要一個可能的ViewSwitcherListener,OnViewChange等來做什麼,它只能在持有ViewSwitcher/ViewFlipper的Activity中實現。由於您沒有發佈任何代碼示例,因此很難說哪些更好,哪些不更好。 – kaspermoerch

+0

我在代碼中添加了代碼示例。我打算嘗試onFocusChange(),因爲它獲得了參數:'v':狀態已更改的視圖。 - 'hasFocus' v。 的新焦點狀態但我還沒有測試過任何東西。 – Xazen