我正在實施一個帶有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方法因爲該應用程序被關閉
嘗試在System.out行之前調用super.onBackPressed。 – Razgriz
@Razgriz已經嘗試過,但沒有任何反應 – n4h1n
可能重複[問題與onActivityResult在選項卡活動](http://stackoverflow.com/questions/11556684/issue-with-onactivityresult-in-tab-activity) –