2013-07-31 163 views
1

我正在實施一個帶有Tabhost的應用程序。在我的一個標籤中,我正在執行多個活動(ActivityGroup)。無法覆蓋onBackPressed onKeyDown - Android

例如:我在活動之一,在點擊一個listview後,我去同一個選項卡中的活動二。問題是當我按回按鈕,應用程序關閉,我想回到活動之一。我已經覆蓋onBackPressed()方法,onKeyDown方法,他們不會被調用,他們不會被解僱。我見過很多帖子都有同樣的問題,但是顯示的任何解決方案都無法正常工作。

會很高興爲您的幫助!

創建標籤:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_tab); 

    TabHost fichas = getTabHost(); 
    fichas.setup();  

    TabHost.TabSpec spec=fichas.newTabSpec("tab1"); 
    Intent i = new Intent(getApplicationContext(), AllProductsActivity.class); 
    spec.setContent(i); 
    spec.setIndicator("Hoy"); 
    fichas.addTab(spec); 

    TabHost.TabSpec spec2=fichas.newTabSpec("tab2"); 
    Intent i2 = new Intent(getApplicationContext(), QueActivity.class); 
    spec2.setContent(i2); 
    spec2.setIndicator("Que"); 
    fichas.addTab(spec2); 

    spec=fichas.newTabSpec("tab3"); 
    spec.setContent(R.id.Cuando); 
    spec.setIndicator("Cuando"); 
    fichas.addTab(spec); 

    spec=fichas.newTabSpec("tab4"); 
    spec.setContent(R.id.Donde); 
    spec.setIndicator("Donde"); 
    fichas.addTab(spec); 

} 

在QueActivity.class我執行onClickListener顯示在同一選項卡中的新活動:

public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
      //creo el nuevo objeto para poder llamarlo 

      Intent intent = new Intent(QueActivity.this, ProductosCategorias.class); 

      //Creo la informacion para pasar entre actividades 
      Bundle informacion= new Bundle(); 
      intent.putExtra("eventos",categorias.get(position).getEventos()); 
      Toast.makeText(getApplicationContext(), "has seleccionado "+ categorias.get(position).getNombre(), Toast.LENGTH_SHORT).show(); 
      //New activity 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      View view = (getLocalActivityManager().startActivity("productos", intent)).getDecorView(); 
      setContentView(view);         
     } 

活動提出要求。這裏就是我想覆蓋onBackPressed()

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_productos_categorias); 
    //Do stuff here 
    }); 

外onCreate方法我正在改寫:

public void onBackPressed(){ 
    System.out.println("Fires"); 
    //Go back to the other intent 
} 

問題是,當我按下返回鍵我沒有得到我的overrided方法因爲該應用程序被關閉

+0

嘗試在System.out行之前調用super.onBackPressed。 – Razgriz

+0

@Razgriz已經嘗試過,但沒有任何反應 – n4h1n

+0

可能重複[問題與onActivityResult在選項卡活動](http://stackoverflow.com/questions/11556684/issue-with-onactivityresult-in-tab-activity) –

回答

1

這是因爲在ActivityGrouponActivityResult事件會在您的ActivityGroup類上調用,而不是在activity中調用它。

這個堆棧溢出的問題可以解決你的問題:

Issue with onActivityResult in tab activity

+0

問題解決! – n4h1n

0

您的主要活動託管tabwidget正在獲取onBackPressed調用嘗試使用回調接口或檢查當前的活動是否在後退按鈕按下時在前臺。

0

第一,其中u實現onBackPressed,在你tabhost活動(其中包含所有標籤的活動)或你的標籤活動(QueActivity)。

對於子選項卡活動,TabHost Activity(ActivityGroup)不會調用onBackPressed。我認爲它必須手動完成。

TabHost活動(的ActivityGroup)

@Override 
public void onBackPressed(){ 
    Activity currentActivity = getCurrentActivity(); 
    if(currentActivity != null) 
     currentActivity.onBackPressed(); 
} 

與上面的代碼你QueActivity應該得到onBackPressed調用。