2011-06-06 83 views
1

我有一個TabActivity有三個選項卡。拳頭選項卡是問題。第一個選項卡加載一個ActivityGroup。 OnCreate它加載一個默認的內容視圖。稍後在某個事件中,我們添加一個不同的內容視圖。這工作正常,但我的問題是有人在加載第二個內容視圖後按下手機上的後退按鈕。它將它們帶到最後一個Activity,而不是將它們帶到ActivityGroup中添加的第一個內容視圖。我如何重定向後退按鈕以在我的ActivityGroup中調用方法?TabActivity中的多個活動

我這樣構建它,我可能在一個選項卡中有多個視圖(活動)。關於如何將後退按鈕事件重定向到我自己的方法的任何想法?那很簡單,很好。

如果代碼是有幫助的:

public class LiveTabGroup extends ActivityGroup implements MoveToScreenNotification.handler 

{

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    EventBus.subscribe(MoveToScreenNotification.class, this); 

    View view = getLocalActivityManager().startActivity("CameraListView", new Intent(this,CameraListView.class). 
      addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); 

    this.setContentView(view); 

} 

@Override 
public void onMoveToScreenNotification(MoveToScreenNotification notif) 
{ 
    if (notif.newScreen == MoveToScreenNotification.SCREEN_MOVIEPLAYER_LIVE) 
    { 
     SugarLoafSingleton.currentCamera.url = notif.videoURL; 
     // Throw UI management on main thread 
     runOnUiThread(new Runnable(){ 
     public void run() 
     { 
      StartPlayer(); 
     } 
     }); 

    } 

} 

public void StartPlayer() 
{ 
    View view = getLocalActivityManager().startActivity("VideoPlayer", new Intent(this,VideoPlayerView.class). 
      addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); 

    this.addContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT)); 


} 

}

回答

2

我不得不重寫後退按鈕自己。請參閱下面的代碼,爲您做到這一點。基本上,這覆蓋了Android Activity類中的onKeyDown的默認實現。後退按鈕的鍵碼是KeyEvent.KEYCODE_BACK,並且該代碼捕獲該鍵碼。對於其他任何東西,它將只運行默認處理程序:

public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     // Put your custom code for handling the Back button here 

        // Return here (exit function) or else it will run the 
        // default implementation of the Back button 
        return; 
    } 

    return super.onKeyDown(keyCode, event); 
} 
+0

謝謝!現在,如果我想讓其他活動保持其默認後退按鈕行爲,那麼我是否必須在此代碼塊中進行某種檢查?意思是,一旦我覆蓋它總是被覆蓋? – spentak 2011-06-06 15:47:14

+0

它只會在代碼結束時的任何活動中被覆蓋。因此,如果將它放入到TabHost代碼中,則它應該覆蓋TabHost中的所有活動,然後您需要進行一些檢查。 – Jon 2011-06-06 15:57:10